2

I have an application which is multilingual, and I have to convert the VK scan codes to the appropriate character as specified by the current keyboard layout provided that they are mapped to some character representation and not just an action, state or other meta use. How would I accomplish this, such that it is portable across different Windows using different languages?

I could write a mapping function for the VK scan codes, but in the WinUser.h file, I'm reading stuff like:

#define VK_OEM_4          0xDB  //  '[{' for US
#define VK_OEM_5          0xDC  //  '\|' for US
#define VK_OEM_6          0xDD  //  ']}' for US
#define VK_OEM_7          0xDE  //  ''"' for US

Which indicates that if the keyboard isn't US, this could be different. There must be a function that I can call to do the appropriate mapping through the keyboard driver or something, right?

Adrian
  • 10,246
  • 4
  • 44
  • 110
  • Look at the [`MapVirtualKey` function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646306.aspx): "*Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.*" And also the [`MapVirtualKeyEx` function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646307.aspx): "*Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code. The function translates the codes using the input language and an input locale identifier.*" – Remy Lebeau Jun 10 '15 at 04:27

1 Answers1

0

The reliable solution is to process WM_CHAR messages. Separating character input from keystrokes provide the mapping for you, and supports not only built-in Windows keyboard layouts but also IMEs, speech APIs, handwriting and other third party input methods. MapVirtualKey, ToUnicode and related functions rely on internal state in the Windows kernel with respect to deadkeys and so cannot be guaranteed to always return the correct character(s).

Marc Durdin
  • 1,675
  • 2
  • 20
  • 27
  • Interesting... I don't remember the exact context of why I needed this. It might have been that I needed it in the pretranslate handler because that was used to select an item in a multi-selection control when a user pressed a key on the keyboard. It was for that reason that I didn't use the `WM_CHAR` handler. That's assuming that is the reason that I posted this question. – Adrian Jan 18 '18 at 20:05
  • Righto. That's assuming English, then? For Chinese, Japanese, Korean, for example, you can't press a single key to type a letter. – Marc Durdin Jan 19 '18 at 01:34
  • Very true. I'd have to go back and see what I was doing back then. – Adrian Jan 19 '18 at 13:45