@@ -1281,6 +1281,24 @@ static SDL2_Scancode SDL3ScancodeToSDL2Scancode(SDL_Scancode scancode)
1281
1281
}
1282
1282
}
1283
1283
1284
+ static SDL_Keycode SDL3KeycodeToSDL2Keycode (SDL_Scancode scancode , SDL_Keycode keycode )
1285
+ {
1286
+ /* Keys without the extended mask are passed through. */
1287
+ if (!(keycode & SDLK_EXTENDED_MASK )) {
1288
+ return keycode ;
1289
+ }
1290
+
1291
+ /* SDLK_TAB is an ASCII value and can't be converted directly from a scancode. */
1292
+ if (keycode == SDLK_LEFT_TAB ) {
1293
+ return SDLK_TAB ;
1294
+ }
1295
+
1296
+ /* Convert the scancode directly to the keycode. This matches the mapping behavior
1297
+ * of SDL2 backends unless some very esoteric key remapping is being used.
1298
+ */
1299
+ return SDL_SCANCODE_TO_KEYCODE (scancode );
1300
+ }
1301
+
1284
1302
/* (current) strategy for SDL_Events:
1285
1303
in sdl12-compat, we built our own event queue, so when the SDL2 queue is pumped, we
1286
1304
took the events we cared about and added them to the sdl12-compat queue, and otherwise
@@ -1322,7 +1340,7 @@ Event3to2(const SDL_Event *event3, SDL2_Event *event2)
1322
1340
case SDL_EVENT_KEY_DOWN :
1323
1341
case SDL_EVENT_KEY_UP :
1324
1342
event2 -> key .keysym .scancode = SDL3ScancodeToSDL2Scancode (event3 -> key .scancode );
1325
- event2 -> key .keysym .sym = event3 -> key .key ;
1343
+ event2 -> key .keysym .sym = SDL3KeycodeToSDL2Keycode ( event3 -> key .scancode , event3 -> key . key ) ;
1326
1344
event2 -> key .keysym .mod = event3 -> key .mod ;
1327
1345
event2 -> key .state = event3 -> key .down ;
1328
1346
event2 -> key .repeat = event3 -> key .repeat ;
@@ -1902,18 +1920,48 @@ SDL_GetKeyFromScancode(SDL2_Scancode scancode)
1902
1920
}
1903
1921
1904
1922
if (scancode <= SDL2_SCANCODE_MODE ) {
1905
- // They're the same
1906
- return SDL3_GetKeyFromScancode (SDL2ScancodeToSDL3Scancode (scancode ), SDL_KMOD_NONE , true);
1907
- } else {
1908
- return SDL_SCANCODE_TO_KEYCODE (scancode );
1923
+ /* Filter out extended keycodes. This matches the key mapping behavior of SDL2 backends. */
1924
+ const SDL_Keycode keycode = SDL3_GetKeyFromScancode (SDL2ScancodeToSDL3Scancode (scancode ), SDL_KMOD_NONE , true);
1925
+ if (!(keycode & SDLK_EXTENDED_MASK )) {
1926
+ return keycode ;
1927
+ }
1909
1928
}
1929
+
1930
+ return SDL_SCANCODE_TO_KEYCODE (scancode );
1910
1931
}
1911
1932
1912
1933
SDL_DECLSPEC SDL2_Scancode SDLCALL
1913
1934
SDL_GetScancodeFromKey (SDL_Keycode key )
1914
1935
{
1915
1936
SDL_Keymod modstate = SDL_KMOD_NONE ;
1916
1937
SDL_Scancode scancode = SDL3_GetScancodeFromKey (key , & modstate );
1938
+
1939
+ if (scancode == SDL_SCANCODE_UNKNOWN ) {
1940
+ /* Keys commonly remapped to extended keycodes in SDL3.
1941
+ * If the keymap returns no mapping, convert the key to a scancode directly.
1942
+ */
1943
+ switch (key ) {
1944
+ case SDLK_LCTRL :
1945
+ case SDLK_RCTRL :
1946
+ case SDLK_LALT :
1947
+ case SDLK_RALT :
1948
+ case SDLK_LGUI :
1949
+ case SDLK_RGUI :
1950
+ case SDLK_CAPSLOCK :
1951
+ case SDLK_SCROLLLOCK :
1952
+ case SDLK_PRINTSCREEN :
1953
+ case SDLK_INSERT :
1954
+ scancode = (SDL_Scancode )(key & ~SDLK_SCANCODE_MASK );
1955
+ break ;
1956
+ case SDLK_TAB :
1957
+ /* ASCII value that can't be directly converted to a scancode. */
1958
+ scancode = SDL_SCANCODE_TAB ;
1959
+ break ;
1960
+ default :
1961
+ break ;
1962
+ }
1963
+ }
1964
+
1917
1965
if (modstate != SDL_KMOD_NONE ) {
1918
1966
return SDL2_SCANCODE_UNKNOWN ;
1919
1967
}
0 commit comments