Quit if Android back-button is pressed 3 times within 1 second

Interpret fast repeated presses of the back-button (3 times within 1 second) as a quit-event, so the app can be quit cleanly and quickly without using the UI. The client settings are otherwise not saved if the app is closed by minimizing it using the home button and waiting for the OS to kill it or by discarding it in the recent apps view.
This commit is contained in:
Robert Müller 2024-05-09 14:39:31 +02:00
parent bba606feae
commit 91848f0be6
2 changed files with 31 additions and 2 deletions

View file

@ -641,9 +641,35 @@ int CInput::Update()
// handle keys // handle keys
case SDL_KEYDOWN: case SDL_KEYDOWN:
#if defined(CONF_PLATFORM_ANDROID)
if(Event.key.keysym.scancode == KEY_AC_BACK && m_BackButtonReleased)
{
if(m_LastBackPress == -1 || (Now - m_LastBackPress) / (float)time_freq() > 1.0f)
{
m_NumBackPresses = 1;
m_LastBackPress = Now;
}
else
{
m_NumBackPresses++;
if(m_NumBackPresses >= 3)
{
// Quit if the Android back-button was pressed 3 times within 1 second
return 1;
}
}
m_BackButtonReleased = false;
}
#endif
Scancode = TranslateScancode(Event.key); Scancode = TranslateScancode(Event.key);
break; break;
case SDL_KEYUP: case SDL_KEYUP:
#if defined(CONF_PLATFORM_ANDROID)
if(Event.key.keysym.scancode == KEY_AC_BACK && !m_BackButtonReleased)
{
m_BackButtonReleased = true;
}
#endif
Action = IInput::FLAG_RELEASE; Action = IInput::FLAG_RELEASE;
Scancode = TranslateScancode(Event.key); Scancode = TranslateScancode(Event.key);
break; break;

View file

@ -78,8 +78,11 @@ private:
bool m_MouseFocus; bool m_MouseFocus;
bool m_MouseDoubleClick; bool m_MouseDoubleClick;
#if defined(CONF_PLATFORM_ANDROID) // No relative mouse on Android #if defined(CONF_PLATFORM_ANDROID)
ivec2 m_LastMousePos = ivec2(0, 0); ivec2 m_LastMousePos = ivec2(0, 0); // No relative mouse on Android
int m_NumBackPresses = 0;
bool m_BackButtonReleased = true;
int64_t m_LastBackPress = -1;
#endif #endif
// IME support // IME support