4

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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Romka
  • 57
  • 6

1 Answers1

5

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;
Sean
  • 60,939
  • 11
  • 97
  • 136
  • But how to do it if there is no lParam (if it is not in WndProc)? How to convert char to scan code? – Romka Jan 24 '14 at 13:45
  • There is always an `lParam` in your WndProc (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v=vs.85).aspx). You can you're catching via WM_CHAR so just look at the parameter. – Sean Jan 24 '14 at 13:50
  • I mean if i get the char in std::getchar(). Not in WndProc. In WndProc works fine – Romka Jan 24 '14 at 13:51
  • @Romka - can't you just cast the `char` to an `int` ? – Sean Jan 24 '14 at 13:54
  • It won't work, because casted char 'x' to int is 120, not 0x2d(45) – Romka Jan 24 '14 at 14:03
  • Do you have an ideas how to convert? – Romka Jan 24 '14 at 14:15