473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Keyboard Listener

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
9 10943
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steve High | last post by:
Is there a way to use a keyboard listener in applications other than applets? I am trying to make a simple text editor that stores words in a LinkedList, then checks each word in the list against...
1
by: Cherrish Vaidiyan | last post by:
sir, I have a small error in Listener configuration.I have two system with a database in each. I am using Red Hat 9 and Oracle 9i. so i shall anme the database and system. system 1 - node2 ...
5
by: Axel Dachtler | last post by:
Hi, I have a listener problem. The listener cannot read SERVICE_NAME in TNS-Descriptor. The service-name I specified in Oracle Net Manager for this database is testdb as well. ...
3
by: Bill | last post by:
When vb6 Winsock.RemoteHost is set to "127.0.0.1", c# socket listener cannot hear connect request (my old vb6 winsock listener could hear it...). Why doesn't this work, and is there a work...
2
by: Dan | last post by:
I have an application that uses a COM port barcode scanner. This uses a listener to notify the application when a barcode has been scanned. The application now needs to be modified to use a Human...
3
by: YSChong | last post by:
Hi all, I'm just a newbie doing undergrad in Computing. I always face problems when it comes to looking for an appropriate method. Lately, I've been trying to write an application that listens...
6
by: Steve Teeples | last post by:
I have been perplexed by how to best treat an event that spans different classes. For example, I have a form which a user inputs data. I want to broadcast that data via an event to another...
5
by: mivey4 | last post by:
Hi, First off, I am aware that this is a very heavily documented error and I have done my homework for throughly researching probable causes before deciding to post my problem here. At this point,...
3
by: fts2012 | last post by:
Hi all, I want to realize a function which could remember the latest several items in clipboard. I hava alreay known the method get the pressed keys when my program is on active ,but I don't know...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.