diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 370775d4a..2665ee0c5 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -4535,6 +4535,12 @@ int main(int argc, const char **argv) SDL_SetHint("SDL_IME_SHOW_UI", "1"); #endif +#if defined(CONF_PLATFORM_ANDROID) + // Trap the Android back button so it can be handled in our code reliably + // instead of letting the system handle it. + SDL_SetHint("SDL_ANDROID_TRAP_BACK_BUTTON", "1"); +#endif + // init SDL if(SDL_Init(0) < 0) { diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index 34cee2443..5632e1ef1 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -538,6 +538,40 @@ void CInput::SetCompositionWindowPosition(float X, float Y, float H) SDL_SetTextInputRect(&Rect); } +static int TranslateScancode(const SDL_KeyboardEvent &KeyEvent) +{ + // See SDL_Keymod for possible modifiers: + // NONE = 0 + // LSHIFT = 1 + // RSHIFT = 2 + // LCTRL = 64 + // RCTRL = 128 + // LALT = 256 + // RALT = 512 + // LGUI = 1024 + // RGUI = 2048 + // NUM = 4096 + // CAPS = 8192 + // MODE = 16384 + // Sum if you want to ignore multiple modifiers. + if(KeyEvent.keysym.mod & g_Config.m_InpIgnoredModifiers) + { + return 0; + } + + int Scancode = g_Config.m_InpTranslatedKeys ? SDL_GetScancodeFromKey(KeyEvent.keysym.sym) : KeyEvent.keysym.scancode; + +#if defined(CONF_PLATFORM_ANDROID) + // Translate the Android back-button to the escape-key so it can be used to open/close the menu, close popups etc. + if(Scancode == KEY_AC_BACK) + { + Scancode = KEY_ESCAPE; + } +#endif + + return Scancode; +} + int CInput::Update() { const int64_t Now = time_get(); @@ -607,28 +641,11 @@ int CInput::Update() // handle keys case SDL_KEYDOWN: - // See SDL_Keymod for possible modifiers: - // NONE = 0 - // LSHIFT = 1 - // RSHIFT = 2 - // LCTRL = 64 - // RCTRL = 128 - // LALT = 256 - // RALT = 512 - // LGUI = 1024 - // RGUI = 2048 - // NUM = 4096 - // CAPS = 8192 - // MODE = 16384 - // Sum if you want to ignore multiple modifiers. - if(!(Event.key.keysym.mod & g_Config.m_InpIgnoredModifiers)) - { - Scancode = g_Config.m_InpTranslatedKeys ? SDL_GetScancodeFromKey(Event.key.keysym.sym) : Event.key.keysym.scancode; - } + Scancode = TranslateScancode(Event.key); break; case SDL_KEYUP: Action = IInput::FLAG_RELEASE; - Scancode = g_Config.m_InpTranslatedKeys ? SDL_GetScancodeFromKey(Event.key.keysym.sym) : Event.key.keysym.scancode; + Scancode = TranslateScancode(Event.key); break; // handle the joystick events