We can use GetKeyNameText()
to retrieve a string that represents the name of a key. Is there any way to do the reverse, ie get a scancode or virtual key for a given key name?
I want to write key names to a config file so users can edit them easily. When I read in the config file, I would need to do the reverse of GetKeyNameText()
.
Not a good idea, these names are not constant:
The key name is translated according to the layout of the currently installed keyboard, thus the function may give different results for different input locales.
If you are desperate I suppose you can call GetKeyNameText
in a loop trying all possible scancodes.
MapVirtualKey
can convert a scancode to a virtual key if you also need to do that. Virtual keys are stable across all keyboards and all versions of Windows.
I would suggest that you hardcode some of the names that are common and have virtual keys like A-Z, 0-9, Ctrl, Alt, Shift, Home, Insert etc. and store those as English text. Only store the scancode for other strange keys.