Converting Scan Codes to ASCII
This article describes how to convert scan codes using Win32 functionality.
- 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.
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) Here's a sample function:
This returns the number of characters extracted. It will return 0 in the case that there was no conversion, or 1 or 2, depending multibyte character sets, for a valid output.static int scan2ascii(DWORD scancode, ushort* result)
{
static HKL layout=GetKeyboardLayout(0);
static uchar State[256];
if (GetKeyboardState(State)==FALSE)
return 0;
UINT vk=MapVirtualKeyEx(scancode,1,layout);
return ToAsciiEx(vk,scancode,State,result,0,layout);
}
In most cases, using char(result[0]) would provide the desired output.
Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Photon
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion