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.
This commit is contained in:
UnknownShadow200 2020-12-03 14:53:26 +11:00
parent a5c6fbbea2
commit 461d8bf252

View File

@ -3319,11 +3319,9 @@ static EM_BOOL OnKey(int type, const EmscriptenKeyboardEvent* ev, void* data) {
if (Key_IsAltPressed() || Key_IsWinPressed()) return true; if (Key_IsAltPressed() || Key_IsWinPressed()) return true;
if (Key_IsControlPressed() && key != 'C' && key != 'V') return true; if (Key_IsControlPressed() && key != 'C' && key != 'V') return true;
/* Space needs special handling, as intercepting prevents the key press event */ /* Space needs special handling, as intercepting this prevents the ' ' key press event */
/* But on mobile Safari, space (on external keyboard) still scrolls the page */ /* But on Safari, space scrolls the page - so need to intercept when keyboard is NOT open */
/* Desktop - Space is never intercepted */ if (key == KEY_SPACE) return !keyboardOpen;
/* Mobile - Space is intercepted when keyboard is NOT open */
if (key == KEY_SPACE) return Input_TouchMode && !keyboardOpen;
/* Must not intercept KeyDown for regular keys, otherwise KeyPress doesn't get raised. */ /* 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. */ /* 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 */ /* - This causes problems such as attempting to backspace all text later to */
/* not actually backspace everything. (because the HTML text input does not */ /* not actually backspace everything. (because the HTML text input does not */
/* have these intercepted key presses in its text buffer) */ /* 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)) { if (Convert_TryCodepointToCP437(ev->charCode, &keyChar)) {
Event_RaiseInt(&InputEvents.Press, 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) { void Window_OpenKeyboard(const cc_string* text, int type) {
char str[NATIVE_STR_LEN]; char str[NATIVE_STR_LEN];
if (!Input_TouchMode) return;
keyboardOpen = true; keyboardOpen = true;
if (!Input_TouchMode) return;
Platform_ConvertString(str, text); Platform_ConvertString(str, text);
Platform_LogConst("OPEN SESAME"); Platform_LogConst("OPEN SESAME");
@ -3674,8 +3672,8 @@ void Window_SetKeyboardText(const cc_string* text) {
} }
void Window_CloseKeyboard(void) { void Window_CloseKeyboard(void) {
if (!Input_TouchMode) return;
keyboardOpen = false; keyboardOpen = false;
if (!Input_TouchMode) return;
EM_ASM({ EM_ASM({
if (!window.cc_inputElem) return; if (!window.cc_inputElem) return;