• Advertisement
Sign in to follow this  

Getting the key name for a VK constant

This topic is 2410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :(

Share this post


Link to post
Share on other sites
Advertisement
[quote name='SyncViews' timestamp='1308160253' post='4823709']
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 :(
[/quote]

I looked for something like this myself and I couldn't find anything so I would also be interested in this information =)

Share this post


Link to post
Share on other sites
You can get the character by using [url="http://msdn.microsoft.com/en-us/library/ms646306%28v=vs.85%29.aspx"]MapVirtualKey[/url], 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.

Share this post


Link to post
Share on other sites
This is probably what you're looking for: [url="http://msdn.microsoft.com/en-us/library/ms646300.aspx"]MSDN[/url]

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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement