Home All Groups Group Topic Archive Search About

Reading keyboard's key names (Internalization)

Author
9 Aug 2010 4:40 AM
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.

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

Author
9 Aug 2010 10:36 AM
Henning
Show quote Hide quote
"Satchmo" <don.tspa.m@dontdo.spam.not> skrev i meddelandet
news:i3o0se$b94$1@news.eternal-september.org...
> 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)?

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

>
> 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'

Show quoteHide quote
>
> 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
Author
9 Aug 2010 11:40 AM
Satchmo
Henning wrote:
> Satchmo wrote:
>>
>> 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
>

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?

>> 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?
Author
9 Aug 2010 12:19 PM
Henning
Show quote Hide quote
"Satchmo" <don.tspa.m@dontdo.spam.not> skrev i meddelandet
news:i3opf5$vke$1@news.eternal-september.org...
> Henning wrote:
>> Satchmo wrote:
>>>
>>> 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
>>
>
> 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?
>
>>> 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 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......

>
> Just curious, can you confirm these are the labels on your Swedish
> keyboard's keys?

/Henning
Author
13 Aug 2010 4:09 AM
Satchmo
Henning wrote:
Show quoteHide quote
> Satchmo wrote:
>> 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 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?
Author
9 Aug 2010 1:24 PM
Nobody
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