MapVirtualKey
/
MapVirtualKeyEx
is unreliable when working with extended keys. Some virtual key codes are not specified as extended keys when they should be. Because of this, it can not distinguish between navigational (e.g. Left, Home, PageDown, etc.), Insert, and Delete keys of the Numpad and grey key sets. Even though MSDN states that
MAPVK_VK_TO_VSC_EX
supports extended keys. Also,
MAPVK_VSC_TO_VK_EX
does not actually support extended keys. i.e. it works just like
MAPVK_VSC_TO_VK
.
If it's only for the Numpad keys, virtual key code
VK_NUMPAD0
to
VK_NUMPAD9
are not extended keys. Navigational keys such as Home, Left, and PageDown keys whether they are from the grey or Numpad key groups, their virtual codes are
VK_HOME
,
VK_LEFT
,
VK_NEXT
, etc., and are extended keys. When using
SendInput()
,
KEYEVENTF_EXTENDEDKEY
flag must be specified if the key is an extended key.
If you want a reliable key code convertion function, my suggestion is to create a function which looks up the virtual code and scan code mapping directly from the keyboard layout DLL for the current keyboard layout. Below article explains how to load the module and retrieve the pointer to a Keyboard Layer Descriptor (
KBDTABLES
).
Loading keyboard layout (KbdLayerDescriptor) in 32/64-bit environment
The Keyboard Layer Descriptor structure contains 3 pointers to mapping tables. One for non extended keys (
usVSCtoVK
), and two for extended keys (
VSCtoVK_E0
and
VSCtoVK_E1
). There are two extended key mapping tables since an extended key can have either a
0xE0
or
0xE1
scan code prefix. Non extended keys do not have a scan code prefix.