Discussion:
Reading keyboard's key names (Internalization)
(too old to reply)
Satchmo
2010-08-09 04:40:42 UTC
Permalink
Hi all! I'm trying to make my little app "international-aware" and I'm
using the GetKeyNameText() function to retrieve the name of a key. The
function works good in getting the key names based on the current
keyboard layout. The reason I'm doing this is because users of my app
will be able to access the settings to customize the program's hotkeys,
so I would like to display these in the user's language.

For example, the following key name retrieval work:

GetKeyText(vbKeyControl ) -> "Ctrl"
GetKeyText(vbKeyShift ) -> "Shift"
GetKeyText(vbKeyMenu ) -> "Alt"

However, the following don't:

GetKeyText(vbKeyAdd ) -> "Num +"
GetKeyText(vbKeySnapshot) -> "Sys Req"

Questions:

1. Why does GetKeyNameText() return "Num +" instead of "+" (the actual
label of the key)?

2. Why does GetKeyNameText() return "Sys Req" instead of "Print Scrn"
(the actual label of the key)?

Below the code I'm using, thanks in advance for any help! -Satchmo
--
Private Declare Function GetKeyNameText Lib "user32" Alias
"GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal
nSize As Long) As Long

Private Declare Function MapVirtualKey Lib "user32" Alias
"MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Public Function GetKeyText(KeyCode As VBRUN.KeyCodeConstants) As String

Const MAXBUFFERSIZE As Long = 256
Dim buf As String
Dim chars As Long
Dim ScanCode As Long

ScanCode = MapVirtualKey(KeyCode, 0)

buf = Space$(MAXBUFFERSIZE)
chars = GetKeyNameText((ScanCode * 2^16), buf, MAXBUFFERSIZE - 1)
GetKeyText = Left(buf, chars)

End Function
Henning
2010-08-09 10:36:21 UTC
Permalink
Post by Satchmo
Hi all! I'm trying to make my little app "international-aware" and I'm
using the GetKeyNameText() function to retrieve the name of a key. The
function works good in getting the key names based on the current keyboard
layout. The reason I'm doing this is because users of my app will be able
to access the settings to customize the program's hotkeys, so I would like
to display these in the user's language.
GetKeyText(vbKeyControl ) -> "Ctrl"
GetKeyText(vbKeyShift ) -> "Shift"
GetKeyText(vbKeyMenu ) -> "Alt"
GetKeyText(vbKeyAdd ) -> "Num +"
GetKeyText(vbKeySnapshot) -> "Sys Req"
1. Why does GetKeyNameText() return "Num +" instead of "+" (the actual
label of the key)?
To distinguish it from the '+' key in the alpha part of the keyboard.
Wich has:
Unshifted = +
Shifted = ?
AltGr = \ AltGr is the same as Ctrl+Alt
Post by Satchmo
2. Why does GetKeyNameText() return "Sys Req" instead of "Print Scrn" (the
actual label of the key)?
There are dual labels on my swedish keyboard: 'Print Scrn' / 'SysRq'
Post by Satchmo
Below the code I'm using, thanks in advance for any help! -Satchmo
--
Private Declare Function GetKeyNameText Lib "user32" Alias
"GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal
nSize As Long) As Long
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA"
(ByVal wCode As Long, ByVal wMapType As Long) As Long
Public Function GetKeyText(KeyCode As VBRUN.KeyCodeConstants) As String
Const MAXBUFFERSIZE As Long = 256
Dim buf As String
Dim chars As Long
Dim ScanCode As Long
ScanCode = MapVirtualKey(KeyCode, 0)
buf = Space$(MAXBUFFERSIZE)
chars = GetKeyNameText((ScanCode * 2^16), buf, MAXBUFFERSIZE - 1)
GetKeyText = Left(buf, chars)
End Function
/Henning
Satchmo
2010-08-09 11:40:15 UTC
Permalink
Post by Henning
Post by Satchmo
1. Why does GetKeyNameText() return "Num +" instead of "+" (the actual
label of the key)?
To distinguish it from the '+' key in the alpha part of the keyboard.
Unshifted = +
Shifted = ?
AltGr = \ AltGr is the same as Ctrl+Alt
OK, but that key has a *unique* label "+" which may differ in other
languages I guess. How do I retrieve the actual localized label instead
of just hard-coding "+" into the program?
Post by Henning
Post by Satchmo
2. Why does GetKeyNameText() return "Sys Req" instead of "Print Scrn" (the
actual label of the key)?
There are dual labels on my swedish keyboard: 'Print Scrn' / 'SysRq'
My English keyboard also has that key as dual-labeled. How do I capture
the "Print Scrn" text of that key?

Just curious, I just switched from my English input locale to Swedish
(LCID &H041D) using the APIs MapVirtualKeyEx() and LoadKeyboardLayout()
and I got the following outputs (verbatim by letter/case):

Virtual Key English Swedish
--------------------------------
vbKeyControl Ctrl CTRL
vbKeyShift Shift SKIFT
vbKeyMenu Alt ALT
vbKeySnapshot Sys Req Sys Req
vbKeyBack Backspace BACKSTEG
vbKeyReturn Enter RETUR

Just curious, can you confirm these are the labels on your Swedish
keyboard's keys?
Henning
2010-08-09 12:19:19 UTC
Permalink
Post by Satchmo
Post by Henning
Post by Satchmo
1. Why does GetKeyNameText() return "Num +" instead of "+" (the actual
label of the key)?
To distinguish it from the '+' key in the alpha part of the keyboard.
Unshifted = +
Shifted = ?
AltGr = \ AltGr is the same as Ctrl+Alt
OK, but that key has a *unique* label "+" which may differ in other
languages I guess. How do I retrieve the actual localized label instead of
just hard-coding "+" into the program?
Post by Henning
Post by Satchmo
2. Why does GetKeyNameText() return "Sys Req" instead of "Print Scrn" (the
actual label of the key)?
There are dual labels on my swedish keyboard: 'Print Scrn' / 'SysRq'
My English keyboard also has that key as dual-labeled. How do I capture
the "Print Scrn" text of that key?
Just curious, I just switched from my English input locale to Swedish
(LCID &H041D) using the APIs MapVirtualKeyEx() and LoadKeyboardLayout()
Virtual Key English Swedish
--------------------------------
vbKeyControl Ctrl CTRL is Ctrl
vbKeyShift Shift SKIFT is a bold Up-arrow
vbKeyMenu Alt ALT is Alt
vbKeySnapshot Sys Req Sys Req is SysRq
vbKeyBack Backspace BACKSTEG is a backarrow
vbKeyReturn Enter RETUR is Enter
All labels on my Logitech kb are in english.
Caps Lock, Insert, Delete, Home and so on.
The only diff is the swedish letters ÅÄÖ åäö and possibly the shifted
AlpaNumeric keys.
@ is AltGr+2, $ is AltGr+4, { is AltGr+7, [ is AltGr+8......
Post by Satchmo
Just curious, can you confirm these are the labels on your Swedish
keyboard's keys?
/Henning
Satchmo
2010-08-13 04:09:48 UTC
Permalink
Post by Henning
Post by Satchmo
Just curious, I just switched from my English input locale to Swedish
(LCID&H041D) using the APIs MapVirtualKeyEx() and LoadKeyboardLayout()
Virtual Key English Swedish
--------------------------------
vbKeyControl Ctrl CTRL is Ctrl
vbKeyShift Shift SKIFT is a bold Up-arrow
vbKeyMenu Alt ALT is Alt
vbKeySnapshot Sys Req Sys Req is SysRq
vbKeyBack Backspace BACKSTEG is a backarrow
vbKeyReturn Enter RETUR is Enter
All labels on my Logitech kb are in english.
Caps Lock, Insert, Delete, Home and so on.
The only diff is the swedish letters ÅÄÖ åäö and possibly the shifted
AlpaNumeric keys.
@ is AltGr+2, $ is AltGr+4, { is AltGr+7, [ is AltGr+8......
OK, the function GetKeyNameText() is not accurate or useful then. It was
wonderful to think I could use this function to get the name of the key
(wasn't that the idea (Microsoft's)? That function should be removed
from the API! Any thoughts?

Anyways, I'll just use resource files and put in the name of the
function keys in English for all languages (CTL, ALT, PRINT SCR, ENTER,
+, etc). Any objections anyone?

Nobody
2010-08-09 13:24:12 UTC
Permalink
You can see Keyboard Layout for all countries by visiting this page:

Windows Keyboard Layouts:
http://msdn.microsoft.com/en-us/goglobal/bb964651.aspx

Unfortunately they only include the main keyboard part, not navigation keys,
or numeric keypads.

Other links:

Go Global Developer Center:
http://msdn.microsoft.com/en-us/goglobal/default.aspx
Loading...