WinRT: Fixed bug whereby a right-shift key might get reported as left-shift
authorDavid Ludwig
Fri, 24 Oct 2014 22:09:47 -0400
changeset 91895dc3c7c2864c
parent 9188 480d583ce7ea
child 9190 ff12797f287b
WinRT: Fixed bug whereby a right-shift key might get reported as left-shift
src/video/winrt/SDL_winrtkeyboard.cpp
     1.1 --- a/src/video/winrt/SDL_winrtkeyboard.cpp	Fri Oct 24 21:25:21 2014 -0400
     1.2 +++ b/src/video/winrt/SDL_winrtkeyboard.cpp	Fri Oct 24 22:09:47 2014 -0400
     1.3 @@ -252,15 +252,47 @@
     1.4  }
     1.5  
     1.6  static SDL_Scancode
     1.7 -TranslateKeycode(int keycode)
     1.8 +TranslateKeycode(int keycode, unsigned int nativeScancode)
     1.9  {
    1.10 -    /* Try to get a documented, WinRT, 'VirtualKey' first (as documented at
    1.11 +    // TODO, WinRT: try filling out the WinRT keycode table as much as possible, using the Win32 table for interpretation hints
    1.12 +    //SDL_Log("WinRT TranslateKeycode, keycode=%d\n", (int)keycode);
    1.13 +    SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
    1.14 +
    1.15 +    /* HACK ALERT: At least one VirtualKey constant (Shift) with a left/right
    1.16 +     * designation might not get reported with its correct handedness, however
    1.17 +     * its hardware scan code can fill in the gaps.  If this is detected,
    1.18 +     * use the hardware scan code to try telling if the left, or the right
    1.19 +     * side's key was used.
    1.20 +     *
    1.21 +     * If Microsoft ever allows MapVirtualKey or MapVirtualKeyEx to be used
    1.22 +     * in WinRT apps, or something similar to these (it doesn't appear to be,
    1.23 +     * at least not for Windows [Phone] 8/8.1, as of Oct 24, 2014), then this
    1.24 +     * hack might become deprecated, or obsolete.
    1.25 +     */
    1.26 +    if (nativeScancode < (sizeof(windows_scancode_table) / sizeof(SDL_Scancode))) {
    1.27 +        switch (keycode) {
    1.28 +            case 16:    // VirtualKey.Shift
    1.29 +                switch (windows_scancode_table[nativeScancode]) {
    1.30 +                    case SDL_SCANCODE_LSHIFT:
    1.31 +                    case SDL_SCANCODE_RSHIFT:
    1.32 +                        return windows_scancode_table[nativeScancode];
    1.33 +                }
    1.34 +                break;
    1.35 +            
    1.36 +            // Add others, as necessary.
    1.37 +            //
    1.38 +            // Unfortunately, this hack doesn't seem to work in determining
    1.39 +            // handedness with Control keys.
    1.40 +
    1.41 +            default:
    1.42 +                break;
    1.43 +        }
    1.44 +    }
    1.45 +
    1.46 +    /* Try to get a documented, WinRT, 'VirtualKey' next (as documented at
    1.47         http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.virtualkey.aspx ).
    1.48         If that fails, fall back to a Win32 virtual key.
    1.49      */
    1.50 -    // TODO, WinRT: try filling out the WinRT keycode table as much as possible, using the Win32 table for interpretation hints
    1.51 -    //SDL_Log("WinRT TranslateKeycode, keycode=%d\n", (int)keycode);
    1.52 -    SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
    1.53      if (keycode < SDL_arraysize(WinRT_Official_Keycodes)) {
    1.54          scancode = WinRT_Official_Keycodes[keycode];
    1.55      }
    1.56 @@ -276,10 +308,10 @@
    1.57  void
    1.58  WINRT_ProcessKeyDownEvent(Windows::UI::Core::KeyEventArgs ^args)
    1.59  {
    1.60 -    SDL_Scancode sdlScancode = TranslateKeycode((int)args->VirtualKey);
    1.61 +    SDL_Scancode sdlScancode = TranslateKeycode((int)args->VirtualKey, args->KeyStatus.ScanCode);
    1.62  #if 0
    1.63      SDL_Keycode keycode = SDL_GetKeyFromScancode(sdlScancode);
    1.64 -    SDL_Log("key down, handled=%s, ext?=%s, released?=%s, menu key down?=%s, repeat count=%d, native scan code=%d, was down?=%s, vkey=%d, sdl scan code=%d (%s), sdl key code=%d (%s)\n",
    1.65 +    SDL_Log("key down, handled=%s, ext?=%s, released?=%s, menu key down?=%s, repeat count=%d, native scan code=0x%x, was down?=%s, vkey=%d, sdl scan code=%d (%s), sdl key code=%d (%s)\n",
    1.66          (args->Handled ? "1" : "0"),
    1.67          (args->KeyStatus.IsExtendedKey ? "1" : "0"),
    1.68          (args->KeyStatus.IsKeyReleased ? "1" : "0"),
    1.69 @@ -301,10 +333,10 @@
    1.70  void
    1.71  WINRT_ProcessKeyUpEvent(Windows::UI::Core::KeyEventArgs ^args)
    1.72  {
    1.73 -    SDL_Scancode sdlScancode = TranslateKeycode((int)args->VirtualKey);
    1.74 +    SDL_Scancode sdlScancode = TranslateKeycode((int)args->VirtualKey, args->KeyStatus.ScanCode);
    1.75  #if 0
    1.76      SDL_Keycode keycode = SDL_GetKeyFromScancode(sdlScancode);
    1.77 -    SDL_Log("key up, handled=%s, ext?=%s, released?=%s, menu key down?=%s, repeat count=%d, native scan code=%d, was down?=%s, vkey=%d, sdl scan code=%d (%s), sdl key code=%d (%s)\n",
    1.78 +    SDL_Log("key up, handled=%s, ext?=%s, released?=%s, menu key down?=%s, repeat count=%d, native scan code=0x%x, was down?=%s, vkey=%d, sdl scan code=%d (%s), sdl key code=%d (%s)\n",
    1.79          (args->Handled ? "1" : "0"),
    1.80          (args->KeyStatus.IsExtendedKey ? "1" : "0"),
    1.81          (args->KeyStatus.IsKeyReleased ? "1" : "0"),