From 461d8bf252a9bdeb5bd927f3562840e695ea6ed0 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 3 Dec 2020 14:53:26 +1100 Subject: [PATCH] Webclient: Space should always have its default action prevented unless text input keyboard is 'open' This change is necessary because Safari scrolls the page when you press space and the default action is not prevented. However, we also can't just always prevent the default action, because then the key press event for ' ' would never get raised for text input. So this seems like a reasonable compromise. --- src/Window.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Window.c b/src/Window.c index 8ac790b8e..06460ebd8 100644 --- a/src/Window.c +++ b/src/Window.c @@ -3319,11 +3319,9 @@ static EM_BOOL OnKey(int type, const EmscriptenKeyboardEvent* ev, void* data) { if (Key_IsAltPressed() || Key_IsWinPressed()) return true; if (Key_IsControlPressed() && key != 'C' && key != 'V') return true; - /* Space needs special handling, as intercepting prevents the key press event */ - /* But on mobile Safari, space (on external keyboard) still scrolls the page */ - /* Desktop - Space is never intercepted */ - /* Mobile - Space is intercepted when keyboard is NOT open */ - if (key == KEY_SPACE) return Input_TouchMode && !keyboardOpen; + /* Space needs special handling, as intercepting this prevents the ' ' key press event */ + /* But on Safari, space scrolls the page - so need to intercept when keyboard is NOT open */ + if (key == KEY_SPACE) return !keyboardOpen; /* Must not intercept KeyDown for regular keys, otherwise KeyPress doesn't get raised. */ /* However, do want to prevent browser's behaviour on F11, F5, home etc. */ @@ -3343,7 +3341,7 @@ static EM_BOOL OnKeyPress(int type, const EmscriptenKeyboardEvent* ev, void* dat /* - This causes problems such as attempting to backspace all text later to */ /* not actually backspace everything. (because the HTML text input does not */ /* have these intercepted key presses in its text buffer) */ - if (keyboardOpen) return false; + if (Input_TouchMode && keyboardOpen) return false; if (Convert_TryCodepointToCP437(ev->charCode, &keyChar)) { Event_RaiseInt(&InputEvents.Press, keyChar); @@ -3625,8 +3623,8 @@ EMSCRIPTEN_KEEPALIVE void Window_OnTextChanged(const char* src) { void Window_OpenKeyboard(const cc_string* text, int type) { char str[NATIVE_STR_LEN]; - if (!Input_TouchMode) return; keyboardOpen = true; + if (!Input_TouchMode) return; Platform_ConvertString(str, text); Platform_LogConst("OPEN SESAME"); @@ -3674,8 +3672,8 @@ void Window_SetKeyboardText(const cc_string* text) { } void Window_CloseKeyboard(void) { - if (!Input_TouchMode) return; keyboardOpen = false; + if (!Input_TouchMode) return; EM_ASM({ if (!window.cc_inputElem) return;