Getting the key name for a VK constant

Started by
3 comments, last by SyncViews 12 years, 10 months ago
Given a VK constant value (say VK_CONTROL, 0x11) I want to get a human readable string for display to the end user, e.g. "Ctrl". Previously I have done this with an array of strings so that I could do like "keyNames[VK_CONTROL]". However some of the symbol keys move around with keyboard layouts and I suspect there are some other cases I overlooked. And it seems to me that using a hardcodded table for this sort of thing is the wrong way to do it anyway.

I suspect that the Windows API contains a function to convert from a VK code to a "whats printed on the physical keyboard" string, however I have not been able to find it :(
Advertisement

Given a VK constant value (say VK_CONTROL, 0x11) I want to get a human readable string for display to the end user, e.g. "Ctrl". Previously I have done this with an array of strings so that I could do like "keyNames[VK_CONTROL]". However some of the symbol keys move around with keyboard layouts and I suspect there are some other cases I overlooked. And it seems to me that using a hardcodded table for this sort of thing is the wrong way to do it anyway.

I suspect that the Windows API contains a function to convert from a VK code to a "whats printed on the physical keyboard" string, however I have not been able to find it :(


I looked for something like this myself and I couldn't find anything so I would also be interested in this information =)
You can get the character by using MapVirtualKey, but that's not exactly what you're looking for obviously (it wouldn't do anything for Control).

I'm pretty sure this is outside the scope of the Windows API and you should just keep doing it the way you're doing it.
Anthony Umfer
This is probably what you're looking for: MSDN

However I find your statement that virtual keys shift around with keyboard layouts weird. That's the whole point of the virtual keys, they are defined constants and do NOT move around. The layout adjustment is done on a lower level, so a fixed string array should work fine.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Well I don't know about the physical locations of a given VK_ code, allthough I guess they also move around given that a UK keyboard has a key between Shift annd 'Z' while US ones don't, instead having a key beteen backspace and return but what symbols some of the VK_OEM_* codes represent definatly changes.

e.g. Apperently VK_OEM_3 on a US keyboard is the ` ~ key. However for me on a UK layout those are not even on the same key. ` is to the left of '1' with the ¬ and ¦ (shift and alt gr respectivily) and uses the VK_OEM_8 code, and ~ is a shifted # to the left of the return key using VK_OEM_7. Not sure what VK_OEM_8 is meant to be for US users, but VK_OEM_7 is listed as ' ". Well ' is what VK_OEM_3 here (with @ as the shifted symbol) and " is a shifted 2.

Havnt tested yet but I'm guessing MapVirtualKey can deal with all the VK_OEM's in a suitable manner. Things like ctrl are always ctrl so I'm not too worried about those ones :)

This topic is closed to new replies.

Advertisement