locked
Dealing with Keyboard layouts in C++/DirectX 11 games

    Question

  • Hi all,

    We're working on a game for Windows 8 and we noticed that functions like MapVirtualKey have been removed from the allowed Windows 8 Store APIs. What is the best way to deal with getting key scan codes, or at the very least mapping virtual keys to an ANSI layout?

    Our current solution only deals with specific cases we know of, but we can't seem to find a general solution.

    Just a bit more detail in case the problem isn't clear. Our game allows for movement using WASD as well as the arrow keys. We use CoreWindow's KeyDown and KeyUp to catch the key press events. When switching my keyboard layout in Windows to French, it shouldn't be WASD, but rather ZQSD. Windows API used to include functions like MapVirtualKey and VkKeyScan to allow you to translate to a standard ANSI layout and report them as WASD. These functions have been defined out for Windows Store apps.

    Thanks in advance!

    David Goemans

    -----------------------

    Game Developer

    Codeglue

    Friday, November 09, 2012 10:56 AM

Answers

  • Yes, that is correct. There are several different standards for scan-code mapping, so different keyboard types can have different scan-codes for the same key.

    For your use I would skip the scan codes and check the VK codes. You can localize your defaults based on the current keyboard and provide an option for the user to change to something else.

    --Rob

    Wednesday, November 14, 2012 1:35 AM
    Owner

All replies

  • I found something! It seems that KeyEventArgs you get back from CoreWindow's KeyDown event has a KeyStatus.ScanCode. This ScanCode maps onto physical keys.

    Will this always work?

    ----------------------------

    David Goemans

    Game Developer 

    Codeglue

    Friday, November 09, 2012 11:46 AM
  • Yes, the virtual key may be different, but the scan code are the same.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 12, 2012 2:54 AM
    Moderator
  • Thanks Jesse.

    Does this apply to all keyboards? I read more online, and it seems that USB keyboards and older PS2 or other keyboards might report different scan codes. Is that correct?

    Thanks,

    David

    Monday, November 12, 2012 8:33 AM
  • Hello,
     
    Ok, I will involve more experts to investigate it.
     
    Best regards,
    Jesse

    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 12, 2012 10:57 AM
    Moderator
  • Yes, that is correct. There are several different standards for scan-code mapping, so different keyboard types can have different scan-codes for the same key.

    For your use I would skip the scan codes and check the VK codes. You can localize your defaults based on the current keyboard and provide an option for the user to change to something else.

    --Rob

    Wednesday, November 14, 2012 1:35 AM
    Owner