By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,995 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,995 IT Pros & Developers. It's quick & easy.

Keyboard Listener

P: n/a
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
Nov 16 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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" wrote:
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
Nov 16 '07 #2

P: n/a
On Nov 16, 3:10 pm, ruben <ru...@discussions.microsoft.comwrote:
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" wrote:
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 !?
Nov 16 '07 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Kbalz" <Ku************@gmail.comwrote in message
news:c6**********************************@y5g2000h sf.googlegroups.com...
On Nov 16, 3:10 pm, ruben <ru...@discussions.microsoft.comwrote:
>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" wrote:
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 !?
Nov 17 '07 #4

P: n/a
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...lobalHooks.asp

Hope that helps.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comschrieb
im Newsbeitrag news:28**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"Kbalz" <Ku************@gmail.comwrote in message
news:c6**********************************@y5g2000h sf.googlegroups.com...
>On Nov 16, 3:10 pm, ruben <ru...@discussions.microsoft.comwrote:
>>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" wrote:
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 !?
Nov 17 '07 #5

P: n/a
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/2...14/481082.aspx
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Christoph Hausner" <ch***************@hotmail.dewrote in message
news:ei**************@TK2MSFTNGP02.phx.gbl...
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...lobalHooks.asp

Hope that helps.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comschrieb
im Newsbeitrag news:28**********************************@microsof t.com...
> 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]
- mv*@spam.guard.caspershouse.com

"Kbalz" <Ku************@gmail.comwrote in message
news:c6**********************************@y5g2000 hsf.googlegroups.com...
>>On Nov 16, 3:10 pm, ruben <ru...@discussions.microsoft.comwrote:
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" wrote:
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 !?
Nov 17 '07 #6

P: n/a
On Nov 17, 11:21 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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/2...14/481082.aspx

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Christoph Hausner" <christoph_haus...@hotmail.dewrote in message

news:ei**************@TK2MSFTNGP02.phx.gbl...
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...lobalHooks.asp
Hope that helps.
"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.comschrieb
im Newsbeitragnews:28******************************** **@microsoft.com...
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]
- m...@spam.guard.caspershouse.com
"Kbalz" <Kurtas.Balc...@gmail.comwrote in message
news:c6**********************************@y5g2000 hsf.googlegroups.com...
On Nov 16, 3:10 pm, ruben <ru...@discussions.microsoft.comwrote:
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" wrote:
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!
Nov 17 '07 #7

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Kbalz" <Ku************@gmail.comwrote in message
news:5b**********************************@e4g2000h sg.googlegroups.com...
On Nov 17, 11:21 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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/2...14/481082.aspx

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Christoph Hausner" <christoph_haus...@hotmail.dewrote in message

news:ei**************@TK2MSFTNGP02.phx.gbl...
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...lobalHooks.asp
Hope that helps.
"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com>
schrieb
im
Newsbeitragnews:28******************************** **@microsoft.com...
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]
- m...@spam.guard.caspershouse.com
>"Kbalz" <Kurtas.Balc...@gmail.comwrote in message
news:c6**********************************@y5g200 0hsf.googlegroups.com...
On Nov 16, 3:10 pm, ruben <ru...@discussions.microsoft.comwrote:
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" wrote:
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!
Nov 18 '07 #8

P: n/a
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.DefaultInput Language.Culture.EnglishName);
comboBox1.SelectedItem =
InputLanguage.DefaultInputLanguage.Culture.English Name;
}
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
Dec 12 '07 #9

P: n/a
On Dec 12, 12:36 am, "Rajkiran R.B." <rajkiran...@hotmail.comwrote:
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.DefaultInput Language.Culture.EnglishN-ame);
comboBox1.SelectedItem =
InputLanguage.DefaultInputLanguage.Culture.English Name;
}

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
Dec 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.