KeyDown Handler Question

M

Marvin Addison

I am attempting to create a simple component that provides visual
feedback for the key(s) that are currently being pressed on the
keyboard. The component extends Panel and draws fine. In order to
highlight the currently pressed key(s), I need to add a KeyDown
handler to the component. Simple enough.

The devil is in the details. I cannot distinguish between left/right
Control, Alt, or Shift keys, nor can I distinguish between Enter on
the main keypad and Enter on the number pad. For example
KeyEventArgs.KeyCode reports Keys.ControlKey, Keys.Menu,
Keys.ShiftKey, and Keys.Enter regardless of which key in each pair is
pressed. Likewise, the KeyEventArgs.KeyValue gives the same integer
for each pair of these keys. That is not specific enough! I need to
know _exactly_ which key was pressed.

Can I get this information from the KeyDown event, or another
event/method that is accessible from a Windows Forms application in
C#? If not, could I hook into a Win32 API event message queue to get
this information? If that's the only way, could someone please
provide concrete code examples or documentation on how to do this from
C#?

Thanks,
Marvin
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Marvin Addison) scripsit:
The devil is in the details. I cannot distinguish between left/right
Control, Alt, or Shift keys, nor can I distinguish between Enter on
the main keypad and Enter on the number pad. For example
KeyEventArgs.KeyCode reports Keys.ControlKey, Keys.Menu,
Keys.ShiftKey, and Keys.Enter regardless of which key in each pair is
pressed. Likewise, the KeyEventArgs.KeyValue gives the same integer
for each pair of these keys. That is not specific enough! I need to
know _exactly_ which key was pressed.

Can I get this information from the KeyDown event, or another
event/method that is accessible from a Windows Forms application in
C#? If not, could I hook into a Win32 API event message queue to get
this information? If that's the only way, could someone please
provide concrete code examples or documentation on how to do this from
C#?

A sample in VB.NET for a global keyboard hook:

<http://www.developer.com/net/net/article.php/11087_2193301_1>
 

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

Top