views:

540

answers:

1

Is there a way to translate a character to its corresponding hardware scan code (not the virtual key code)? I need that to communicate with ancient hardware device.

A: 

Seems the most direct method would be to use MapVirtualKey or MapVirtualKeyEx which translates from VK-codes to Scan codes.

The character to VK scan code can be gotten using VkKeyScan (extracting the low byte which contains the VK Code according to MS documentation). So to get the scan code of 'X':

 UINT VKCode=LOBYTE(VkKeyScan('X'));
 UINT ScanCode=MapVirtualKey(VKCode,0);
Elemental
A more complete answer is, to use VkKeyScan to convert the key to the virtualkeycode, and then with MapVirtualKey convert it to the scancode
Elazar Leibovich
Changed my code to reflect this improvement - thanks Elazar
Elemental
Actually tried this in real life and then had problems; after rereading the MS docs I added the LOBYTE to the solution code above
Elemental