Howto distinguish the Return- and Enter keys

H

Harald

Hi everybody!
In a GridView I have a key down handler like:

CtrlDataGridView_KeyDown(object sender, KeyEventArgs e) {
switch (e.KeyValue) {
case Keys.Return:
....

case Keys.Enter: // ERROR! same value as Keys.Return!
...
}
}

How can I distinguish the return key from the numpads enter key???
Thx in advance, Harald
 
K

Kevin Spencer

You got my curiosity up, so I did some research for you. Both keys have the
exact same value. The only way to tell which physical key was pressed is to
use some unmanaged interop, using the Windows API. There are 2 specific
WinApi functions you need to call. The first function registers your app to
receive Raw Input messages (otherwise, they are ignored). Fortunately for
us, there is a great web site (Wiki) devoted to documenting the Windows API
for use in .Net (using PInvoke): http://www.pinvoke.net

I rolled up my sleeves, and did some work. The first function
(RegisterRawInputDevices) requires a structure to be passed to it. Again,
fortunately, there is an example of such a structure using the Mouse. Using
the Microsoft WinApi reference, I was able to put together one for the
Keyboard (See the web site for how to use):

private void RegisterKeyboard()
{
WinUserApi.RAWINPUTDEVICE device;

device.WindowHandle = this.Handle;
device.UsagePage = 0x01;
device.Usage = 0x06;
device.Flags = WinUserApi.RawInputDeviceFlags.InputSink |
WinUserApi.RawInputDeviceFlags.NoLegacy;

WinUserApi.RegisterRawInputDevices(device);
}

Note that I have my own static class which I call "WinUserApi" for these
sorts of things. You will want to create your own.

The second one (GetRawInputData) is more straightforward, although they both
use a number of structures and constants (again, these are on the web site).
You won't need to create your own RAWINPUTDEVICE to use it.

After running my app locally and performing some watches, I found only one
difference between the 2 keys. It is in the RAWINPUTKEYBOARD structure, and
it is the Flags member, which has a value of "KeyMake" for the main keyboard
key, and "KeyE0" for the number pad key.

Anyway, here are the references I used:

Raw Input WinApi functions:
http://www.pinvoke.net/search.aspx?search=keyboard&namespace=[All]

RegisterRawInputDevices function:
http://www.pinvoke.net/default.aspx/user32/RegisterRawInputDevices.html

GetRawInputData function:
http://www.pinvoke.net/default.aspx/user32/GetRawInputData.html

Microsoft WinApi Raw Input reference:
http://msdn.microsoft.com/library/e...erinterface/userinput/rawinput.asp?frame=true

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.
 

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