From c7f5d0a71d7318ccc4c3f28ab90d688a1a0868b3 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 23 Aug 2013 18:03:05 +0300 Subject: fix keyboard handling in gui in SDL2. --- src/gui/sdlinput.cpp | 43 +++++++++++++++++++++++-------------------- src/gui/sdlinput.h | 3 +++ src/inputmanager.cpp | 27 +++++++++++++++++++++++++-- src/keyboardconfig.cpp | 15 +++++++++++++++ src/sdlshared.h | 2 +- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/gui/sdlinput.cpp b/src/gui/sdlinput.cpp index fd72cb962..19ec87f02 100644 --- a/src/gui/sdlinput.cpp +++ b/src/gui/sdlinput.cpp @@ -60,6 +60,7 @@ #include "inputmanager.h" #include "keydata.h" +#include "logger.h" #include "mouseinput.h" #include "sdlshared.h" @@ -133,34 +134,16 @@ void SDLInput::pushInput(const SDL_Event &event) { case SDL_KEYDOWN: { - keyInput.setKey(gcn::Key(convertKeyCharacter(event))); keyInput.setType(gcn::KeyInput::PRESSED); - keyInput.setShiftPressed(event.key.keysym.mod & KMOD_SHIFT); - keyInput.setControlPressed(event.key.keysym.mod & KMOD_CTRL); - keyInput.setAltPressed(event.key.keysym.mod & KMOD_ALT); - keyInput.setMetaPressed(event.key.keysym.mod & KMOD_META); - keyInput.setNumericPad(event.key.keysym.sym >= SDLK_KP0 - && event.key.keysym.sym <= SDLK_KP_EQUALS); - const int actionId = inputManager.getActionByKey(event); - if (actionId >= 0) - keyInput.setActionId(actionId); + convertKeyEventToKey(event, keyInput); mKeyInputQueue.push(keyInput); break; } case SDL_KEYUP: { - keyInput.setKey(gcn::Key(convertKeyCharacter(event))); keyInput.setType(gcn::KeyInput::RELEASED); - keyInput.setShiftPressed(event.key.keysym.mod & KMOD_SHIFT); - keyInput.setControlPressed(event.key.keysym.mod & KMOD_CTRL); - keyInput.setAltPressed(event.key.keysym.mod & KMOD_ALT); - keyInput.setMetaPressed(event.key.keysym.mod & KMOD_META); - keyInput.setNumericPad(event.key.keysym.sym >= SDLK_KP0 - && event.key.keysym.sym <= SDLK_KP_EQUALS); - const int actionId = inputManager.getActionByKey(event); - if (actionId >= 0) - keyInput.setActionId(actionId); + convertKeyEventToKey(event, keyInput); mKeyInputQueue.push(keyInput); break; } @@ -249,6 +232,26 @@ void SDLInput::pushInput(const SDL_Event &event) } // end switch } +void SDLInput::convertKeyEventToKey(const SDL_Event &event, KeyInput &keyInput) +{ + keyInput.setKey(gcn::Key(convertKeyCharacter(event))); + keyInput.setShiftPressed(event.key.keysym.mod & KMOD_SHIFT); + keyInput.setControlPressed(event.key.keysym.mod & KMOD_CTRL); + keyInput.setAltPressed(event.key.keysym.mod & KMOD_ALT); + keyInput.setMetaPressed(event.key.keysym.mod & KMOD_META); +#ifdef USE_SDL2 + const int code = event.key.keysym.scancode; + keyInput.setNumericPad((code >= SDL_SCANCODE_KP_DIVIDE + && code <= SDL_SCANCODE_KP_PERIOD) || code == SDL_SCANCODE_KP_EQUALS); +#else + const int code = event.key.keysym.sym; + keyInput.setNumericPad(code >= SDLK_KP0 && code <= SDLK_KP_EQUALS); +#endif + const int actionId = inputManager.getActionByKey(event); + if (actionId >= 0) + keyInput.setActionId(actionId); +} + int SDLInput::convertMouseButton(const int button) { switch (button) diff --git a/src/gui/sdlinput.h b/src/gui/sdlinput.h index aa02e9554..cbe5863ed 100644 --- a/src/gui/sdlinput.h +++ b/src/gui/sdlinput.h @@ -189,6 +189,9 @@ protected: */ static int convertKeyCharacter(const SDL_Event &event) A_WARN_UNUSED; + static void convertKeyEventToKey(const SDL_Event &event, + KeyInput &keyInput); + std::queue mKeyInputQueue; std::queue mMouseInputQueue; diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index b0a31b442..6e421498e 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -212,10 +212,21 @@ void InputManager::resetKeys() const KeyData &kd = keyData[i]; KeyItem &val0 = key.values[0]; val0.type = kd.defaultType1; - val0.value = kd.defaultValue1; KeyItem &val1 = key.values[1]; val1.type = kd.defaultType2; +#ifdef USE_SDL2 + if (kd.defaultType1 == INPUT_KEYBOARD) + val0.value = SDL_GetScancodeFromKey(kd.defaultValue1); + else + val0.value = kd.defaultValue1; + if (kd.defaultType2 == INPUT_KEYBOARD) + val1.value = SDL_GetScancodeFromKey(kd.defaultValue2); + else + val1.value = kd.defaultValue2; +#else + val0.value = kd.defaultValue1; val1.value = kd.defaultValue2; +#endif } } @@ -233,10 +244,22 @@ void InputManager::makeDefault(const int i) const KeyData &kd = keyData[i]; KeyItem &val0 = key.values[0]; val0.type = kd.defaultType1; - val0.value = kd.defaultValue1; KeyItem &val1 = key.values[1]; val1.type = kd.defaultType2; + +#ifdef USE_SDL2 + if (kd.defaultType1 == INPUT_KEYBOARD) + val0.value = SDL_GetScancodeFromKey(kd.defaultValue1); + else + val0.value = kd.defaultValue1; + if (kd.defaultType2 == INPUT_KEYBOARD) + val1.value = SDL_GetScancodeFromKey(kd.defaultValue2); + else + val1.value = kd.defaultValue2; +#else + val0.value = kd.defaultValue1; val1.value = kd.defaultValue2; +#endif update(); } diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index d0c56ec1d..2bc33fc41 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -61,10 +61,14 @@ void KeyboardConfig::deinit() int KeyboardConfig::getKeyValueFromEvent(const SDL_Event &event) const { +#ifdef USE_SDL2 + return event.key.keysym.scancode; +#else if (event.key.keysym.sym) return event.key.keysym.sym; else if (event.key.keysym.scancode > 1) return -event.key.keysym.scancode; +#endif return 0; } @@ -84,7 +88,14 @@ std::string KeyboardConfig::getKeyName(const int key) if (key == Input::KEY_NO_VALUE) return ""; if (key >= 0) + { +#ifdef USE_SDL2 + return SDL_GetKeyName(SDL_GetKeyFromScancode( + static_cast(key))); +#else return SDL_GetKeyName(static_cast(key)); +#endif + } // TRANSLATORS: long key name, should be short return strprintf(_("key_%d"), key); @@ -138,7 +149,11 @@ std::string KeyboardConfig::getKeyShortString(const std::string &key) SDLKey KeyboardConfig::getKeyFromEvent(const SDL_Event &event) { +#ifdef USE_SDL2 + return event.key.keysym.scancode; +#else return event.key.keysym.sym; +#endif } KeysVector *KeyboardConfig::getActionVector(const SDL_Event &event) diff --git a/src/sdlshared.h b/src/sdlshared.h index d06040220..7683cb9f2 100644 --- a/src/sdlshared.h +++ b/src/sdlshared.h @@ -24,7 +24,7 @@ #ifdef USE_SDL2 #define SDL_GetKeyState SDL_GetKeyboardState -#define SDLKey SDL_Keycode +#define SDLKey SDL_Scancode #define SDL_keysym SDL_Keysym #define SDL_ANYFORMAT 0 -- cgit v1.2.3-70-g09d2