How to convert VK scan codes to appropriate character for language selected

1.9k views Asked by At

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?

1

There are 1 answers

3
Marc Durdin On

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).