Translate back-button to escape-key on Android

Translate the Android back-button to the escape-key, so it can be used to navigate back in menus, open/close the ingame menu, close the editor etc.

Trap the Android back button by setting the `SDL_ANDROID_TRAP_BACK_BUTTON` hint, so it can be handled in our code reliably instead of letting the system handle it.
This commit is contained in:
Robert Müller 2024-05-09 00:06:18 +02:00
parent 0f5b14c043
commit bba606feae
2 changed files with 42 additions and 19 deletions

View file

@ -4535,6 +4535,12 @@ int main(int argc, const char **argv)
SDL_SetHint("SDL_IME_SHOW_UI", "1"); SDL_SetHint("SDL_IME_SHOW_UI", "1");
#endif #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 // init SDL
if(SDL_Init(0) < 0) if(SDL_Init(0) < 0)
{ {

View file

@ -538,6 +538,40 @@ void CInput::SetCompositionWindowPosition(float X, float Y, float H)
SDL_SetTextInputRect(&Rect); 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() int CInput::Update()
{ {
const int64_t Now = time_get(); const int64_t Now = time_get();
@ -607,28 +641,11 @@ int CInput::Update()
// handle keys // handle keys
case SDL_KEYDOWN: case SDL_KEYDOWN:
// See SDL_Keymod for possible modifiers: Scancode = TranslateScancode(Event.key);
// 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;
}
break; break;
case SDL_KEYUP: case SDL_KEYUP:
Action = IInput::FLAG_RELEASE; Action = IInput::FLAG_RELEASE;
Scancode = g_Config.m_InpTranslatedKeys ? SDL_GetScancodeFromKey(Event.key.keysym.sym) : Event.key.keysym.scancode; Scancode = TranslateScancode(Event.key);
break; break;
// handle the joystick events // handle the joystick events