Cleaned up WindowsScanCodeToSDLScanCode() so VKeytoScancode() always takes precedence for the keys it handles and the rest of the logic is easier to read.
authorSam Lantinga
Sat, 12 Aug 2017 12:56:28 -0700
changeset 1124270ae86f64702
parent 11241 33802c660fa0
child 11243 54f46b251fe1
Cleaned up WindowsScanCodeToSDLScanCode() so VKeytoScancode() always takes precedence for the keys it handles and the rest of the logic is easier to read.
src/video/windows/SDL_windowsevents.c
     1.1 --- a/src/video/windows/SDL_windowsevents.c	Sat Jun 03 09:13:08 2017 +0200
     1.2 +++ b/src/video/windows/SDL_windowsevents.c	Sat Aug 12 12:56:28 2017 -0700
     1.3 @@ -143,70 +143,74 @@
     1.4  WindowsScanCodeToSDLScanCode(LPARAM lParam, WPARAM wParam)
     1.5  {
     1.6      SDL_Scancode code;
     1.7 -    char bIsExtended;
     1.8      int nScanCode = (lParam >> 16) & 0xFF;
     1.9 +    SDL_bool bIsExtended = (lParam & (1 << 24)) != 0;
    1.10  
    1.11 -    /* 0x45 here to work around both pause and numlock sharing the same scancode, so use the VK key to tell them apart */
    1.12 -    if (nScanCode == 0 || nScanCode == 0x45) {
    1.13 -        return VKeytoScancode(wParam);
    1.14 -    }
    1.15 +    code = VKeytoScancode(wParam);
    1.16  
    1.17 -    if (nScanCode > 127)
    1.18 -        return SDL_SCANCODE_UNKNOWN;
    1.19 -
    1.20 -    code = windows_scancode_table[nScanCode];
    1.21 +    if (code == SDL_SCANCODE_UNKNOWN && nScanCode <= 127) {
    1.22 +        code = windows_scancode_table[nScanCode];
    1.23  
    1.24 -    bIsExtended = (lParam & (1 << 24)) != 0;
    1.25 -    if (!bIsExtended) {
    1.26 -        switch (code) {
    1.27 -        case SDL_SCANCODE_HOME:
    1.28 -            return SDL_SCANCODE_KP_7;
    1.29 -        case SDL_SCANCODE_UP:
    1.30 -            return SDL_SCANCODE_KP_8;
    1.31 -        case SDL_SCANCODE_PAGEUP:
    1.32 -            return SDL_SCANCODE_KP_9;
    1.33 -        case SDL_SCANCODE_LEFT:
    1.34 -            return SDL_SCANCODE_KP_4;
    1.35 -        case SDL_SCANCODE_RIGHT:
    1.36 -            return SDL_SCANCODE_KP_6;
    1.37 -        case SDL_SCANCODE_END:
    1.38 -            return SDL_SCANCODE_KP_1;
    1.39 -        case SDL_SCANCODE_DOWN:
    1.40 -            return SDL_SCANCODE_KP_2;
    1.41 -        case SDL_SCANCODE_PAGEDOWN:
    1.42 -            return SDL_SCANCODE_KP_3;
    1.43 -        case SDL_SCANCODE_INSERT:
    1.44 -            return SDL_SCANCODE_KP_0;
    1.45 -        case SDL_SCANCODE_DELETE:
    1.46 -            return SDL_SCANCODE_KP_PERIOD;
    1.47 -        case SDL_SCANCODE_PRINTSCREEN:
    1.48 -            return SDL_SCANCODE_KP_MULTIPLY;
    1.49 -        default:
    1.50 -            break;
    1.51 -        }
    1.52 -        /* prefer virtual keycodes over scancodes for extended keys */
    1.53 -    } else {
    1.54 -        SDL_Scancode vc = VKeytoScancode(wParam);
    1.55 -        if (vc != SDL_SCANCODE_UNKNOWN) {
    1.56 -            code = vc;
    1.57 +        if (bIsExtended) {
    1.58 +            switch (code) {
    1.59 +            case SDL_SCANCODE_RETURN:
    1.60 +                code = SDL_SCANCODE_KP_ENTER;
    1.61 +                break;
    1.62 +            case SDL_SCANCODE_LALT:
    1.63 +                code = SDL_SCANCODE_RALT;
    1.64 +                break;
    1.65 +            case SDL_SCANCODE_LCTRL:
    1.66 +                code = SDL_SCANCODE_RCTRL;
    1.67 +                break;
    1.68 +            case SDL_SCANCODE_SLASH:
    1.69 +                code = SDL_SCANCODE_KP_DIVIDE;
    1.70 +                break;
    1.71 +            case SDL_SCANCODE_CAPSLOCK:
    1.72 +                code = SDL_SCANCODE_KP_PLUS;
    1.73 +                break;
    1.74 +            default:
    1.75 +                break;
    1.76 +            }
    1.77          } else {
    1.78              switch (code) {
    1.79 -            case SDL_SCANCODE_RETURN:
    1.80 -                return SDL_SCANCODE_KP_ENTER;
    1.81 -            case SDL_SCANCODE_LALT:
    1.82 -                return SDL_SCANCODE_RALT;
    1.83 -            case SDL_SCANCODE_LCTRL:
    1.84 -                return SDL_SCANCODE_RCTRL;
    1.85 -            case SDL_SCANCODE_SLASH:
    1.86 -                return SDL_SCANCODE_KP_DIVIDE;
    1.87 -            case SDL_SCANCODE_CAPSLOCK:
    1.88 -                return SDL_SCANCODE_KP_PLUS;
    1.89 +            case SDL_SCANCODE_HOME:
    1.90 +                code = SDL_SCANCODE_KP_7;
    1.91 +                break;
    1.92 +            case SDL_SCANCODE_UP:
    1.93 +                code = SDL_SCANCODE_KP_8;
    1.94 +                break;
    1.95 +            case SDL_SCANCODE_PAGEUP:
    1.96 +                code = SDL_SCANCODE_KP_9;
    1.97 +                break;
    1.98 +            case SDL_SCANCODE_LEFT:
    1.99 +                code = SDL_SCANCODE_KP_4;
   1.100 +                break;
   1.101 +            case SDL_SCANCODE_RIGHT:
   1.102 +                code = SDL_SCANCODE_KP_6;
   1.103 +                break;
   1.104 +            case SDL_SCANCODE_END:
   1.105 +                code = SDL_SCANCODE_KP_1;
   1.106 +                break;
   1.107 +            case SDL_SCANCODE_DOWN:
   1.108 +                code = SDL_SCANCODE_KP_2;
   1.109 +                break;
   1.110 +            case SDL_SCANCODE_PAGEDOWN:
   1.111 +                code = SDL_SCANCODE_KP_3;
   1.112 +                break;
   1.113 +            case SDL_SCANCODE_INSERT:
   1.114 +                code = SDL_SCANCODE_KP_0;
   1.115 +                break;
   1.116 +            case SDL_SCANCODE_DELETE:
   1.117 +                code = SDL_SCANCODE_KP_PERIOD;
   1.118 +                break;
   1.119 +            case SDL_SCANCODE_PRINTSCREEN:
   1.120 +                code = SDL_SCANCODE_KP_MULTIPLY;
   1.121 +                break;
   1.122              default:
   1.123                  break;
   1.124              }
   1.125          }
   1.126      }
   1.127 -
   1.128      return code;
   1.129  }
   1.130