Skip to content

Commit

Permalink
Numpad scancodes have non-numpad keycodes
Browse files Browse the repository at this point in the history
This allows the numpad to work as the user expects based on the numlock state. If the application needs to distinguish the keys, it can check to see whether the scancode is a numpad key or not.
  • Loading branch information
slouken committed Jul 1, 2024
1 parent 121ecf0 commit 93c1292
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 326 deletions.
40 changes: 0 additions & 40 deletions include/SDL3/SDL_keycode.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,8 @@ typedef Uint32 SDL_Keycode;
#define SDLK_DOWN 0x40000051u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_DOWN) */
#define SDLK_UP 0x40000052u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_UP) */
#define SDLK_NUMLOCKCLEAR 0x40000053u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_NUMLOCKCLEAR) */
#define SDLK_KP_DIVIDE 0x40000054u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_DIVIDE) */
#define SDLK_KP_MULTIPLY 0x40000055u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_MULTIPLY) */
#define SDLK_KP_MINUS 0x40000056u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_MINUS) */
#define SDLK_KP_PLUS 0x40000057u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_PLUS) */
#define SDLK_KP_ENTER 0x40000058u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_ENTER) */
#define SDLK_KP_1 0x40000059u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_1) */
#define SDLK_KP_2 0x4000005au /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_2) */
#define SDLK_KP_3 0x4000005bu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_3) */
#define SDLK_KP_4 0x4000005cu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_4) */
#define SDLK_KP_5 0x4000005du /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_5) */
#define SDLK_KP_6 0x4000005eu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_6) */
#define SDLK_KP_7 0x4000005fu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_7) */
#define SDLK_KP_8 0x40000060u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_8) */
#define SDLK_KP_9 0x40000061u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_9) */
#define SDLK_KP_0 0x40000062u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_0) */
#define SDLK_KP_PERIOD 0x40000063u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_PERIOD) */
#define SDLK_APPLICATION 0x40000065u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_APPLICATION) */
#define SDLK_POWER 0x40000066u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_POWER) */
#define SDLK_KP_EQUALS 0x40000067u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_EQUALS) */
#define SDLK_F13 0x40000068u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_F13) */
#define SDLK_F14 0x40000069u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_F14) */
#define SDLK_F15 0x4000006au /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_F15) */
Expand All @@ -196,8 +179,6 @@ typedef Uint32 SDL_Keycode;
#define SDLK_MUTE 0x4000007fu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_MUTE) */
#define SDLK_VOLUMEUP 0x40000080u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_VOLUMEUP) */
#define SDLK_VOLUMEDOWN 0x40000081u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_VOLUMEDOWN) */
#define SDLK_KP_COMMA 0x40000085u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_COMMA) */
#define SDLK_KP_EQUALSAS400 0x40000086u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_EQUALSAS400) */
#define SDLK_ALTERASE 0x40000099u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_ALTERASE) */
#define SDLK_SYSREQ 0x4000009au /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_SYSREQ) */
#define SDLK_CANCEL 0x4000009bu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_CANCEL) */
Expand All @@ -216,32 +197,11 @@ typedef Uint32 SDL_Keycode;
#define SDLK_DECIMALSEPARATOR 0x400000b3u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_DECIMALSEPARATOR) */
#define SDLK_CURRENCYUNIT 0x400000b4u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_CURRENCYUNIT) */
#define SDLK_CURRENCYSUBUNIT 0x400000b5u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_CURRENCYSUBUNIT) */
#define SDLK_KP_LEFTPAREN 0x400000b6u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_LEFTPAREN) */
#define SDLK_KP_RIGHTPAREN 0x400000b7u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_RIGHTPAREN) */
#define SDLK_KP_LEFTBRACE 0x400000b8u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_LEFTBRACE) */
#define SDLK_KP_RIGHTBRACE 0x400000b9u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_RIGHTBRACE) */
#define SDLK_KP_TAB 0x400000bau /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_TAB) */
#define SDLK_KP_BACKSPACE 0x400000bbu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_BACKSPACE) */
#define SDLK_KP_A 0x400000bcu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_A) */
#define SDLK_KP_B 0x400000bdu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_B) */
#define SDLK_KP_C 0x400000beu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_C) */
#define SDLK_KP_D 0x400000bfu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_D) */
#define SDLK_KP_E 0x400000c0u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_E) */
#define SDLK_KP_F 0x400000c1u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_F) */
#define SDLK_KP_XOR 0x400000c2u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_XOR) */
#define SDLK_KP_POWER 0x400000c3u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_POWER) */
#define SDLK_KP_PERCENT 0x400000c4u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_PERCENT) */
#define SDLK_KP_LESS 0x400000c5u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_LESS) */
#define SDLK_KP_GREATER 0x400000c6u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_GREATER) */
#define SDLK_KP_AMPERSAND 0x400000c7u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_AMPERSAND) */
#define SDLK_KP_DBLAMPERSAND 0x400000c8u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_DBLAMPERSAND) */
#define SDLK_KP_VERTICALBAR 0x400000c9u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_VERTICALBAR) */
#define SDLK_KP_DBLVERTICALBAR 0x400000cau /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_DBLVERTICALBAR) */
#define SDLK_KP_COLON 0x400000cbu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_COLON) */
#define SDLK_KP_HASH 0x400000ccu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_HASH) */
#define SDLK_KP_SPACE 0x400000cdu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_SPACE) */
#define SDLK_KP_AT 0x400000ceu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_AT) */
#define SDLK_KP_EXCLAM 0x400000cfu /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_EXCLAM) */
#define SDLK_KP_MEMSTORE 0x400000d0u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_MEMSTORE) */
#define SDLK_KP_MEMRECALL 0x400000d1u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_MEMRECALL) */
#define SDLK_KP_MEMCLEAR 0x400000d2u /* SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KP_MEMCLEAR) */
Expand Down
4 changes: 2 additions & 2 deletions src/events/SDL_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ static SDL_Keycode SDL_GetEventKeycode(SDL_Keyboard *keyboard, SDL_Scancode scan
{
SDL_bool shifted = (modstate & SDL_KMOD_SHIFT) != 0;

// We won't be applying any modifiers by default
modstate = SDL_KMOD_NONE;
// We won't be applying any modifiers except numlock by default
modstate &= SDL_KMOD_NUM;

if (scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_Z) {
if (keyboard->non_latin_letters && (keyboard->keycode_options & KEYCODE_OPTION_LATIN_LETTERS)) {
Expand Down
87 changes: 84 additions & 3 deletions src/events/SDL_keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,92 @@ SDL_Keycode SDL_GetDefaultKeyFromScancode(SDL_Scancode scancode, SDL_Keymod mods
}
}

if (scancode == SDL_SCANCODE_DELETE) {
switch (scancode) {
case SDL_SCANCODE_DELETE:
return SDLK_DELETE;
case SDL_SCANCODE_KP_DIVIDE:
return SDLK_SLASH;
case SDL_SCANCODE_KP_MULTIPLY:
return SDLK_ASTERISK;
case SDL_SCANCODE_KP_MINUS:
return SDLK_MINUS;
case SDL_SCANCODE_KP_PLUS:
return SDLK_PLUS;
case SDL_SCANCODE_KP_ENTER:
return SDLK_RETURN;
case SDL_SCANCODE_KP_1:
return (modstate & SDL_KMOD_NUM) ? SDLK_1 : SDLK_END;
case SDL_SCANCODE_KP_2:
return (modstate & SDL_KMOD_NUM) ? SDLK_2 : SDLK_DOWN;
case SDL_SCANCODE_KP_3:
return (modstate & SDL_KMOD_NUM) ? SDLK_3 : SDLK_PAGEDOWN;
case SDL_SCANCODE_KP_4:
return (modstate & SDL_KMOD_NUM) ? SDLK_4 : SDLK_LEFT;
case SDL_SCANCODE_KP_5:
return (modstate & SDL_KMOD_NUM) ? SDLK_5 : SDLK_UNKNOWN;
case SDL_SCANCODE_KP_6:
return (modstate & SDL_KMOD_NUM) ? SDLK_6 : SDLK_RIGHT;
case SDL_SCANCODE_KP_7:
return (modstate & SDL_KMOD_NUM) ? SDLK_7 : SDLK_HOME;
case SDL_SCANCODE_KP_8:
return (modstate & SDL_KMOD_NUM) ? SDLK_8 : SDLK_UP;
case SDL_SCANCODE_KP_9:
return (modstate & SDL_KMOD_NUM) ? SDLK_9 : SDLK_PAGEUP;
case SDL_SCANCODE_KP_0:
return (modstate & SDL_KMOD_NUM) ? SDLK_0 : SDLK_INSERT;
case SDL_SCANCODE_KP_PERIOD:
return (modstate & SDL_KMOD_NUM) ? SDLK_PERIOD : SDLK_DELETE;
case SDL_SCANCODE_KP_EQUALS:
return SDLK_EQUALS;
case SDL_SCANCODE_KP_COMMA:
return SDLK_COMMA;
case SDL_SCANCODE_KP_EQUALSAS400:
return SDLK_EQUALS;
case SDL_SCANCODE_KP_LEFTPAREN:
return SDLK_LEFTPAREN;
case SDL_SCANCODE_KP_RIGHTPAREN:
return SDLK_RIGHTPAREN;
case SDL_SCANCODE_KP_LEFTBRACE:
return SDLK_LEFTBRACE;
case SDL_SCANCODE_KP_RIGHTBRACE:
return SDLK_RIGHTBRACE;
case SDL_SCANCODE_KP_TAB:
return SDLK_TAB;
case SDL_SCANCODE_KP_BACKSPACE:
return SDLK_BACKSPACE;
case SDL_SCANCODE_KP_A:
return SDLK_A;
case SDL_SCANCODE_KP_B:
return SDLK_B;
case SDL_SCANCODE_KP_C:
return SDLK_C;
case SDL_SCANCODE_KP_D:
return SDLK_D;
case SDL_SCANCODE_KP_E:
return SDLK_E;
case SDL_SCANCODE_KP_F:
return SDLK_F;
case SDL_SCANCODE_KP_PERCENT:
return SDLK_PERCENT;
case SDL_SCANCODE_KP_LESS:
return SDLK_LESS;
case SDL_SCANCODE_KP_GREATER:
return SDLK_GREATER;
case SDL_SCANCODE_KP_AMPERSAND:
return SDLK_AMPERSAND;
case SDL_SCANCODE_KP_COLON:
return SDLK_COLON;
case SDL_SCANCODE_KP_HASH:
return SDLK_HASH;
case SDL_SCANCODE_KP_SPACE:
return SDLK_SPACE;
case SDL_SCANCODE_KP_AT:
return SDLK_AT;
case SDL_SCANCODE_KP_EXCLAM:
return SDLK_EXCLAIM;
default:
return SDL_SCANCODE_TO_KEYCODE(scancode);
}

return SDL_SCANCODE_TO_KEYCODE(scancode);
}

SDL_Scancode SDL_GetDefaultScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate)
Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/SDL_cocoakeyboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static void UpdateKeymap(SDL_CocoaVideoData *data, SDL_bool send_event)
/* Make sure this scancode is a valid character scancode */
SDL_Scancode scancode = darwin_scancode_table[i];
if (scancode == SDL_SCANCODE_UNKNOWN ||
(SDL_GetDefaultKeyFromScancode(scancode, SDL_KMOD_NONE) & SDLK_SCANCODE_MASK)) {
scancode >= SDL_SCANCODE_CAPSLOCK) {
continue;
}

Expand Down
Loading

0 comments on commit 93c1292

Please sign in to comment.