If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
It is often the case in games, that keyboard input is needed to input text, rather than be used for usual game controls. This poses three basic problems:
Key identifiers in DirectInput are keyboard scan codes.
Various international keyboards map scan codes differently, according to their layout.
The same scan code can be translated to various ASCII codes, depending on the status of the shift key, and other such cases.
To solve this problem, we can use various Win32 API calls to resolve which ASCII code the key should be translated to.
These are the steps we'll use:
We'll use GetKeyboardLayout() to get the current keyboard layout.
GetKeyboardState() will be used to retrieve the state of keys like shift, ctrl, alt, etc.
Then using the current layout and scan code, using MapVirtualKeyEx() will give us a win32 virtual key (UINT type).
The ToAsciiEx() function will finally convert the virtual key to 0 - 2 extended ASCII characters (unsigned short)