Keyboard Listener

  • Thread starter Thread starter Kbalz
  • Start date Start date
K

Kbalz

I have an application that minimizes itself, and I want it to listen
for certain key commands, and when they are pressed, my program can
react to them.

So far I've gotten my application to react as I intend while the
program has focus, but when the application loses focus, the listening
stops.

I am using VS 2008 beta 2, with .net 3.5.


A great example of what I'm after is Google's desktop search, where
you hit control twice, the program appears and you interact with it.

If anyone can provide a place to start or some classes to check out I
would appreciate it.

I started to look at Global Keyboard Hooking last night but it looked
very confusing, but if that is what I need then I will read more
articles. Thanks, KB
 
try GetAsyncKeyState api from user32,
code on top of my mind (not tested):

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);

....
if (GetAsyncKeyState(System.Windows.Forms.Keys.Pause) != 0)
{
//Break-Pause key was pressed
}
....
 
try GetAsyncKeyState api from user32,
code on top of my mind (not tested):

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);

...
if (GetAsyncKeyState(System.Windows.Forms.Keys.Pause) != 0)
{
//Break-Pause key was pressed}

...



Kbalz said:
I have an application that minimizes itself, and I want it to listen
for certain key commands, and when they are pressed, my program can
react to them.
So far I've gotten my application to react as I intend while the
program has focus, but when the application loses focus, the listening
stops.
I am using VS 2008 beta 2, with .net 3.5.
A great example of what I'm after is Google's desktop search, where
you hit control twice, the program appears and you interact with it.
If anyone can provide a place to start or some classes to check out I
would appreciate it.
I started to look at Global Keyboard Hooking last night but it looked
very confusing, but if that is what I need then I will read more
articles. Thanks, KB- Hide quoted text -

- Show quoted text -

I had to use this instead: public static extern short
GetAsyncKeyState(System.Windows.Forms.Keys vKey);

But how do I run that IF statement..? In a seperate thread that waits
in an infinite loop !?
 
That's still not going to work for you, as you would have to poll
constantly to see what the keyboard state was, and that's going to put a
drain on your system.

You were on the right track in your original post. You have to create a
global keyboard hook. This will allow your application to monitor keyboard
events even when your app doesn't have focus.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kbalz said:
try GetAsyncKeyState api from user32,
code on top of my mind (not tested):

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);

...
if (GetAsyncKeyState(System.Windows.Forms.Keys.Pause) != 0)
{
//Break-Pause key was pressed}

...



Kbalz said:
I have an application that minimizes itself, and I want it to listen
for certain key commands, and when they are pressed, my program can
react to them.
So far I've gotten my application to react as I intend while the
program has focus, but when the application loses focus, the listening
stops.
I am using VS 2008 beta 2, with .net 3.5.
A great example of what I'm after is Google's desktop search, where
you hit control twice, the program appears and you interact with it.
If anyone can provide a place to start or some classes to check out I
would appreciate it.
I started to look at Global Keyboard Hooking last night but it looked
very confusing, but if that is what I need then I will read more
articles. Thanks, KB- Hide quoted text -

- Show quoted text -

I had to use this instead: public static extern short
GetAsyncKeyState(System.Windows.Forms.Keys vKey);

But how do I run that IF statement..? In a seperate thread that waits
in an infinite loop !?
 
As far as I know hooks are not supported in .Net. You'll always need an
external unmanaged DLL written in C or any other non-.net language. I've
found two articles which explain it very well:

http://www.codeproject.com/csharp/globalsystemhook.asp
http://www.codeproject.com/cs/system/WilsonSystemGlobalHooks.asp

Hope that helps.

Nicholas Paldino said:
That's still not going to work for you, as you would have to poll
constantly to see what the keyboard state was, and that's going to put a
drain on your system.

You were on the right track in your original post. You have to create
a global keyboard hook. This will allow your application to monitor
keyboard events even when your app doesn't have focus.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kbalz said:
try GetAsyncKeyState api from user32,
code on top of my mind (not tested):

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);

...
if (GetAsyncKeyState(System.Windows.Forms.Keys.Pause) != 0)
{
//Break-Pause key was pressed}

...



:
I have an application that minimizes itself, and I want it to listen
for certain key commands, and when they are pressed, my program can
react to them.

So far I've gotten my application to react as I intend while the
program has focus, but when the application loses focus, the listening
stops.

I am using VS 2008 beta 2, with .net 3.5.

A great example of what I'm after is Google's desktop search, where
you hit control twice, the program appears and you interact with it.

If anyone can provide a place to start or some classes to check out I
would appreciate it.

I started to look at Global Keyboard Hooking last night but it looked
very confusing, but if that is what I need then I will read more
articles. Thanks, KB- Hide quoted text -

- Show quoted text -

I had to use this instead: public static extern short
GetAsyncKeyState(System.Windows.Forms.Keys vKey);

But how do I run that IF statement..? In a seperate thread that waits
in an infinite loop !?
 
Christoph,

Most are not, but keyboard hooks (which is what the OP is looking for)
are, as specified in the following knowledge base article:

http://support.microsoft.com/kb/318804/

And elaborated upon in:

http://blogs.msdn.com/toub/archive/2005/10/14/481082.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Christoph Hausner said:
As far as I know hooks are not supported in .Net. You'll always need an
external unmanaged DLL written in C or any other non-.net language. I've
found two articles which explain it very well:

http://www.codeproject.com/csharp/globalsystemhook.asp
http://www.codeproject.com/cs/system/WilsonSystemGlobalHooks.asp

Hope that helps.

Nicholas Paldino said:
That's still not going to work for you, as you would have to poll
constantly to see what the keyboard state was, and that's going to put a
drain on your system.

You were on the right track in your original post. You have to create
a global keyboard hook. This will allow your application to monitor
keyboard events even when your app doesn't have focus.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kbalz said:
try GetAsyncKeyState api from user32,
code on top of my mind (not tested):

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);

...
if (GetAsyncKeyState(System.Windows.Forms.Keys.Pause) != 0)
{
//Break-Pause key was pressed}

...



:
I have an application that minimizes itself, and I want it to listen
for certain key commands, and when they are pressed, my program can
react to them.

So far I've gotten my application to react as I intend while the
program has focus, but when the application loses focus, the
listening
stops.

I am using VS 2008 beta 2, with .net 3.5.

A great example of what I'm after is Google's desktop search, where
you hit control twice, the program appears and you interact with it.

If anyone can provide a place to start or some classes to check out I
would appreciate it.

I started to look at Global Keyboard Hooking last night but it looked
very confusing, but if that is what I need then I will read more
articles. Thanks, KB- Hide quoted text -

- Show quoted text -

I had to use this instead: public static extern short
GetAsyncKeyState(System.Windows.Forms.Keys vKey);

But how do I run that IF statement..? In a seperate thread that waits
in an infinite loop !?
 
Christoph,

Most are not, but keyboard hooks (which is what the OP is looking for)
are, as specified in the following knowledge base article:

http://support.microsoft.com/kb/318804/

And elaborated upon in:

http://blogs.msdn.com/toub/archive/2005/10/14/481082.aspx

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




As far as I know hooks are not supported in .Net. You'll always need an
external unmanaged DLL written in C or any other non-.net language. I've
found two articles which explain it very well:

Hope that helps.
Nicholas Paldino said:
That's still not going to work for you, as you would have to poll
constantly to see what the keyboard state was, and that's going to put a
drain on your system.
You were on the right track in your original post. You have to create
a global keyboard hook. This will allow your application to monitor
keyboard events even when your app doesn't have focus.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
try GetAsyncKeyState api from user32,
code on top of my mind (not tested):
using System.Runtime.InteropServices;
[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);
...
if (GetAsyncKeyState(System.Windows.Forms.Keys.Pause) != 0)
{
//Break-Pause key was pressed}
...
:
I have an application that minimizes itself, and I want it to listen
for certain key commands, and when they are pressed, my program can
react to them.
So far I've gotten my application to react as I intend while the
program has focus, but when the application loses focus, the
listening
stops.
I am using VS 2008 beta 2, with .net 3.5.
A great example of what I'm after is Google's desktop search, where
you hit control twice, the program appears and you interact with it.
If anyone can provide a place to start or some classes to check out I
would appreciate it.
I started to look at Global Keyboard Hooking last night but it looked
very confusing, but if that is what I need then I will read more
articles. Thanks, KB- Hide quoted text -
- Show quoted text -
I had to use this instead: public static extern short
GetAsyncKeyState(System.Windows.Forms.Keys vKey);
But how do I run that IF statement..? In a seperate thread that waits
in an infinite loop !?- Hide quoted text -

- Show quoted text -

Do you know why .NET doesn't support it yet? Having something like
this built in to the arch. would be super helpful I would think!
 
Kbalz,

It is because you need the ability to export a function from a DLL,
which .NET does not support (one of the links goes into it in more detail,
the second, I believe).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kbalz said:
Christoph,

Most are not, but keyboard hooks (which is what the OP is looking
for)
are, as specified in the following knowledge base article:

http://support.microsoft.com/kb/318804/

And elaborated upon in:

http://blogs.msdn.com/toub/archive/2005/10/14/481082.aspx

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




As far as I know hooks are not supported in .Net. You'll always need an
external unmanaged DLL written in C or any other non-.net language.
I've
found two articles which explain it very well:

Hope that helps.
"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
schrieb
im
NewsbeitragThat's still not going to work for you, as you would have to poll
constantly to see what the keyboard state was, and that's going to put
a
drain on your system.
You were on the right track in your original post. You have to
create
a global keyboard hook. This will allow your application to monitor
keyboard events even when your app doesn't have focus.
try GetAsyncKeyState api from user32,
code on top of my mind (not tested):
using System.Runtime.InteropServices;
[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);
...
if (GetAsyncKeyState(System.Windows.Forms.Keys.Pause) != 0)
{
//Break-Pause key was pressed}

:
I have an application that minimizes itself, and I want it to
listen
for certain key commands, and when they are pressed, my program
can
react to them.
So far I've gotten my application to react as I intend while the
program has focus, but when the application loses focus, the
listening
stops.
I am using VS 2008 beta 2, with .net 3.5.
A great example of what I'm after is Google's desktop search,
where
you hit control twice, the program appears and you interact with
it.
If anyone can provide a place to start or some classes to check
out I
would appreciate it.
I started to look at Global Keyboard Hooking last night but it
looked
very confusing, but if that is what I need then I will read more
articles. Thanks, KB- Hide quoted text -
- Show quoted text -
I had to use this instead: public static extern short
GetAsyncKeyState(System.Windows.Forms.Keys vKey);
But how do I run that IF statement..? In a seperate thread that waits
in an infinite loop !?- Hide quoted text -

- Show quoted text -

Do you know why .NET doesn't support it yet? Having something like
this built in to the arch. would be super helpful I would think!
 
Im trying to design an .NET application which is similar to notepad except
for which it supports multiple languages..

The Application consists of a rich textbox and a combo box

The system will have multiple keyboard languages installed.. And the
application when loading searches for the installed keyboard languages and
shows it in a combo box..

InputLanguage[] lang = new
InputLanguage[InputLanguage.InstalledInputLanguages.Count];

private void Form1_Load(object sender, EventArgs e)
{
InputLanguage.InstalledInputLanguages.CopyTo(lang, 0);
foreach (InputLanguage l in lang)
{
comboBox1.Items.Add(l.Culture.EnglishName);
}
comboBox1.SelectedIndex =
comboBox1.Items.IndexOf(InputLanguage.DefaultInputLanguage.Culture.EnglishName);
comboBox1.SelectedItem =
InputLanguage.DefaultInputLanguage.Culture.EnglishName;
}


Whenever the user changes the language then the current language is also
changed through the following code,,
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
InputLanguage.CurrentInputLanguage =
lang[comboBox1.SelectedIndex];
richTextBox1.Focus();

}

Though the input language is changed the content of the rich text box
remains unaltered...

For Example

when the Input Language Is selected as English(United States) when I press
the keys 1,2,3,4,5,6
I get in the textbox as 123456

when the input language Is selected as French (France) when I press the keys
1,2,3,4,5,6
I get in the textbox as &é"'(-

What I need is that when I change the language from English(United States)
to French (France) the text should also change from 12346 to &é"'(-

Reagrds
Rajkiran
 
Im trying to design an .NET application which is similar to notepad except
for which it supports multiple languages..

The Application consists of a rich textbox and a combo box

The system will have multiple keyboard languages installed.. And the
application when loading searches for the installed keyboard languages and
shows it in a combo box..

InputLanguage[] lang = new
InputLanguage[InputLanguage.InstalledInputLanguages.Count];

private void Form1_Load(object sender, EventArgs e)
{
InputLanguage.InstalledInputLanguages.CopyTo(lang, 0);
foreach (InputLanguage l in lang)
{
comboBox1.Items.Add(l.Culture.EnglishName);
}
comboBox1.SelectedIndex =
comboBox1.Items.IndexOf(InputLanguage.DefaultInputLanguage.Culture.EnglishN-ame);
comboBox1.SelectedItem =
InputLanguage.DefaultInputLanguage.Culture.EnglishName;
}

Whenever the user changes the language then the current language is also
changed through the following code,,
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
InputLanguage.CurrentInputLanguage =
lang[comboBox1.SelectedIndex];
richTextBox1.Focus();

}

Though the input language is changed the content of the rich text box
remains unaltered...

For Example

when the Input Language Is selected as English(United States) when I press
the keys 1,2,3,4,5,6
I get in the textbox as 123456

when the input language Is selected as French (France) when I press the keys
1,2,3,4,5,6
I get in the textbox as &é"'(-

What I need is that when I change the language from English(United States)
to French (France) the text should also change from 12346 to &é"'(-

Reagrds
Rajkiran

Well when I get my code working, I'll post it, and you can build your
project from my skeleton methods. I haven't had time, but I've done
lots of reading and hope to get at it this week
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top