How can I implement a function like std::string VirtualKeyCodeToStdString(UCHAR key)
which returns virtual keys descriptions?
Example: input is VK_CAPITAL
, return value is std::string("Caps Lock")
How can I implement a function like std::string VirtualKeyCodeToStdString(UCHAR key)
which returns virtual keys descriptions?
Example: input is VK_CAPITAL
, return value is std::string("Caps Lock")
A simple way to convert the VK code to a text representation of the key is to:
MapVirtualKey
to convert the VK code to a scan code.GetKeyNameText
to obtain the name of the key.For example:
If you're doing this in response to a
WM_KEYDOWN
or other message that passes the scan code in theLPARAM
you can skip the first two steps, since those are just there to massage the VK code into a properly formatted input forGetKeyNameText
. For more information about the function and the format of the first parameter toGetKeyNameText
see the documentation at MSDNNote: I used the W variant on the API calls, so you'd actually need to use a
std::wstring
to pass the key name, but you can change it easily to use the A version. Also, if you need to pass a keyboard layout to get the proper scan code, you can useMapVirtualKeyEx
.There is no full answer. Thank you everybody for helping. After more research I wrote complete function that converts
virtualKey
tostd::string
description.* std::basic_string < TCHAR > Version: *
std::string version: