Use scancodes instead of keycodes for locale independence.

ONLY US KEYBOARDS SUPPORTED! THAT'S HOW WE DO IT IN AMURRICA
This commit is contained in:
Ronald Kinard 2014-11-13 15:19:39 -06:00
parent 0913bd44af
commit 80891e916d

View file

@ -278,8 +278,157 @@ static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen)
}
}
static INT32 Impl_SDL_Scancode_To_Keycode(SDL_Scancode code)
{
if (code >= SDL_SCANCODE_A && code <= SDL_SCANCODE_Z)
{
// get lowercase ASCII
return code - SDL_SCANCODE_A + 'a';
}
if (code >= SDL_SCANCODE_1 && code <= SDL_SCANCODE_9)
{
return code - SDL_SCANCODE_1 + '1';
}
else if (code == SDL_SCANCODE_0)
{
return '0';
}
if (code >= SDL_SCANCODE_F1 && code <= SDL_SCANCODE_F12)
{
return KEY_F1 + (code - SDL_SCANCODE_F1);
}
switch (code)
{
case SDL_SCANCODE_KP_0:
return KEY_KEYPAD0;
case SDL_SCANCODE_KP_1:
return KEY_KEYPAD1;
case SDL_SCANCODE_KP_2:
return KEY_KEYPAD2;
case SDL_SCANCODE_KP_3:
return KEY_KEYPAD3;
case SDL_SCANCODE_KP_4:
return KEY_KEYPAD4;
case SDL_SCANCODE_KP_5:
return KEY_KEYPAD5;
case SDL_SCANCODE_KP_6:
return KEY_KEYPAD6;
case SDL_SCANCODE_KP_7:
return KEY_KEYPAD7;
case SDL_SCANCODE_KP_8:
return KEY_KEYPAD8;
case SDL_SCANCODE_KP_9:
return KEY_KEYPAD9;
case SDL_SCANCODE_RETURN:
return KEY_ENTER;
case SDL_SCANCODE_ESCAPE:
return KEY_ESCAPE;
case SDL_SCANCODE_BACKSPACE:
return KEY_BACKSPACE;
case SDL_SCANCODE_TAB:
return KEY_TAB;
case SDL_SCANCODE_SPACE:
return KEY_SPACE;
case SDL_SCANCODE_MINUS:
return KEY_MINUS;
case SDL_SCANCODE_EQUALS:
return KEY_EQUALS;
case SDL_SCANCODE_LEFTBRACKET:
return '[';
case SDL_SCANCODE_RIGHTBRACKET:
return ']';
case SDL_SCANCODE_BACKSLASH:
return '\\';
case SDL_SCANCODE_NONUSHASH:
return '#';
case SDL_SCANCODE_SEMICOLON:
return ';';
case SDL_SCANCODE_APOSTROPHE:
return '\'';
case SDL_SCANCODE_GRAVE:
return '`';
case SDL_SCANCODE_COMMA:
return ',';
case SDL_SCANCODE_PERIOD:
return '.';
case SDL_SCANCODE_SLASH:
return '/';
case SDL_SCANCODE_CAPSLOCK:
return KEY_CAPSLOCK;
case SDL_SCANCODE_PRINTSCREEN:
return 0; // undefined?
case SDL_SCANCODE_SCROLLLOCK:
return KEY_SCROLLLOCK;
case SDL_SCANCODE_PAUSE:
return KEY_PAUSE;
case SDL_SCANCODE_INSERT:
return KEY_INS;
case SDL_SCANCODE_HOME:
return KEY_HOME;
case SDL_SCANCODE_PAGEUP:
return KEY_PGUP;
case SDL_SCANCODE_DELETE:
return KEY_DEL;
case SDL_SCANCODE_END:
return KEY_END;
case SDL_SCANCODE_PAGEDOWN:
return KEY_PGDN;
case SDL_SCANCODE_RIGHT:
return KEY_RIGHTARROW;
case SDL_SCANCODE_LEFT:
return KEY_LEFTARROW;
case SDL_SCANCODE_DOWN:
return KEY_DOWNARROW;
case SDL_SCANCODE_UP:
return KEY_UPARROW;
case SDL_SCANCODE_NUMLOCKCLEAR:
return KEY_NUMLOCK;
case SDL_SCANCODE_KP_DIVIDE:
return KEY_KPADSLASH;
case SDL_SCANCODE_KP_MULTIPLY:
return '*'; // undefined?
case SDL_SCANCODE_KP_MINUS:
return KEY_MINUSPAD;
case SDL_SCANCODE_KP_PLUS:
return KEY_PLUSPAD;
case SDL_SCANCODE_KP_ENTER:
return KEY_ENTER;
case SDL_SCANCODE_KP_PERIOD:
return KEY_KPADDEL;
case SDL_SCANCODE_NONUSBACKSLASH:
return '\\';
case SDL_SCANCODE_LSHIFT:
return KEY_LSHIFT;
case SDL_SCANCODE_RSHIFT:
return KEY_RSHIFT;
case SDL_SCANCODE_LCTRL:
return KEY_LCTRL;
case SDL_SCANCODE_RCTRL:
return KEY_RCTRL;
case SDL_SCANCODE_LALT:
return KEY_LALT;
case SDL_SCANCODE_RALT:
return KEY_RALT;
case SDL_SCANCODE_LGUI:
return KEY_LEFTWIN;
case SDL_SCANCODE_RGUI:
return KEY_RIGHTWIN;
default:
break;
}
DBG_Printf("Unknown incoming scancode: %d, represented %c\n",
code,
SDL_GetKeyName(SDL_GetKeyFromScancode(code)));
return 0;
}
//
// Translates the SDL key into SRB2 key
// Translates the SDL scancode into SRB2 key
//
// Deprecated: keycodes in SDL2 are not scancode based.
// They do not work with non-US keyboards anymore.
//
static INT32 SDLatekey(SDL_Keycode sym)
@ -809,7 +958,7 @@ static void Impl_HandleKeyboardEvent(SDL_KeyboardEvent evt, Uint32 type)
{
return;
}
event.data1 = SDLatekey(evt.keysym.sym);
event.data1 = Impl_SDL_Scancode_To_Keycode(evt.keysym.scancode);
if (event.data1) D_PostEvent(&event);
}