Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse op for GetKeyNameText

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().

like image 997
user3700562 Avatar asked Sep 03 '25 03:09

user3700562


1 Answers

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.

like image 119
Anders Avatar answered Sep 05 '25 18:09

Anders