Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings!

To remotely control (move, zoom) a videocamera I send keystrokes to the windows control application.
I use SendInput and MapVirtualKeyEx to translate the virtual key code to the keyboard scan code,
because the extendend keys, like VK_NUMPAD0 to VK_NUMPAD9 and the 4 arrow keys, are used to control the camera.
This works well for the basic keys A-Z 0-9 BS, SPACE, CR, NL and ENTER.
The method to get wScan I use
C++
input.ki.wScan = MapVirtualKeyEx(VkKeyScanA(keycode), MAPVK_VK_TO_VSC, GetKeyboardLayout(0));
As stated, this works well for the basic keys, but the extended keys are not translated correctly.
Currently I maually set the appropriate wScan.

What is the correct method to completely translate virtual keys to keyboard wScan code?

Thanks for hints
Wolf

What I have tried:

C++
if (!raw) {
     input.ki.wScan = MapVirtualKeyEx(VkKeyScanA(keycode), MAPVK_VK_TO_VSC, GetKeyboardLayout(0));
 } else {
     input.ki.wScan = keycode;            // scancode, manually supplied
 }
 input.ki.wVk = 0;                        // virtual key
 input.type = INPUT_KEYBOARD;
 input.ki.time = 200;
 input.ki.dwExtraInfo = 0;
Posted
Updated 14-Jan-19 17:01pm

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900