Discussion:
How to use the GetKeyName API in C#
(too old to reply)
kurotsuke
2005-01-01 22:08:48 UTC
Permalink
Hi,

I'm trying to use the GetKeyName API to get the name of certain keys
(in the system language). For example the names of SHIFT and RETURN
keys as well as the names of same special characters.

I tried to call the API GetKeyName passing the KeyCode of the KeyUP
event but got no result


scancode = (uint) e.KeyCode;
StringBuilder sb= new StringBuilder(260);
int ret=GetKeyNameText(scancode,sb,20);


[DllImport("user32.dll")]
static extern int GetKeyNameText(uint lParam, [Out] StringBuilder
lpString, int nSize);

Sometimes the APi return 0 and sometimes it returns the name of
another character.

Can somebody post an example on how to use it? I could not find
anything worthwhile around.

Thanks.
Chris Taylor
2005-01-02 01:52:22 UTC
Permalink
Hi,

I never tested this, but try using e.KeyValue rather than e.KeyCode.

Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
Post by kurotsuke
Hi,
I'm trying to use the GetKeyName API to get the name of certain keys
(in the system language). For example the names of SHIFT and RETURN
keys as well as the names of same special characters.
I tried to call the API GetKeyName passing the KeyCode of the KeyUP
event but got no result
scancode = (uint) e.KeyCode;
StringBuilder sb= new StringBuilder(260);
int ret=GetKeyNameText(scancode,sb,20);
[DllImport("user32.dll")]
static extern int GetKeyNameText(uint lParam, [Out] StringBuilder
lpString, int nSize);
Sometimes the APi return 0 and sometimes it returns the name of
another character.
Can somebody post an example on how to use it? I could not find
anything worthwhile around.
Thanks.
Chris Taylor
2005-01-02 02:41:43 UTC
Permalink
Hi,

I took a quick look at the MSDN docs regarding this function and put
together the following code that should help you get started.

[DllImport("user32", SetLastError=true, CharSet=CharSet.Unicode)]
static extern int GetKeyNameTextW(uint lParam, StringBuilder lpString,
int nSize);

[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
static extern uint MapVirtualKeyW(uint uCode, uint uMapType);

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
StringBuilder sb= new StringBuilder(260);

uint key = MapVirtualKeyW((uint)e.KeyCode, 0) << 16;
int ret=GetKeyNameTextW(key, sb, 260);

label2.Text = sb.ToString();
}

You will notice that I had to use MapVirtualKey to map the key code and then
when passing the code to the GetKeyNameText function I performed a shift, in
the docs you will see that the scan code needs to be in bits 16..23
therefore I needed to shift the returned code to the correct position.

Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
Post by kurotsuke
Hi,
I'm trying to use the GetKeyName API to get the name of certain keys
(in the system language). For example the names of SHIFT and RETURN
keys as well as the names of same special characters.
I tried to call the API GetKeyName passing the KeyCode of the KeyUP
event but got no result
scancode = (uint) e.KeyCode;
StringBuilder sb= new StringBuilder(260);
int ret=GetKeyNameText(scancode,sb,20);
[DllImport("user32.dll")]
static extern int GetKeyNameText(uint lParam, [Out] StringBuilder
lpString, int nSize);
Sometimes the APi return 0 and sometimes it returns the name of
another character.
Can somebody post an example on how to use it? I could not find
anything worthwhile around.
Thanks.
Loading...