Get scan code from WM_CHAR message


How can I convert a character of any language that I catch via WM_CHAR in WndProc to a keyboard scan code? Like if the button pressed is x it would return 0x2d and etc.


The scan code is in bits 16-23 of the lParam parameter according to the WM_CHAR documentation, so just shift and mask:

int scanCode = (lParam >> 16) & 0xff;

If you’ve got a character you can call OemKeyScan, which puts the scan code in the low byte:

char c='X';
int scanCode=OemKeyScan(c) & 0x0ff;

Answered By – Sean

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published