Fixed duplicate key press/release events on iOS When a hardware keyboard is attached, it can take over 100 ms for the keyboard event to generate text input. In that case we want to record that we recently received a keyboard event so we don't synthesize duplicate virtual key press/release events for the input text. (cherry picked from commit 648de4f9b8ebcf9e794aba2ec2955d129167c48b) (cherry picked from commit 38c63afd64450cab8166485c7f93f2cf6c0fa038)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index 7abefde..5a14f85 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -34,7 +34,8 @@
/* Global keyboard information */
#define KEYBOARD_HARDWARE 0x01
-#define KEYBOARD_AUTORELEASE 0x02
+#define KEYBOARD_VIRTUAL 0x02
+#define KEYBOARD_AUTORELEASE 0x04
typedef struct SDL_Keyboard SDL_Keyboard;
@@ -47,6 +48,7 @@ struct SDL_Keyboard
Uint8 keystate[SDL_NUM_SCANCODES];
SDL_Keycode keymap[SDL_NUM_SCANCODES];
SDL_bool autorelease_pending;
+ Uint32 hardware_timestamp;
};
static SDL_Keyboard SDL_keyboard;
@@ -865,7 +867,9 @@ static int SDL_SendKeyboardKeyInternal(Uint8 source, Uint8 state, SDL_Scancode s
keycode = keyboard->keymap[scancode];
}
- if (source == KEYBOARD_AUTORELEASE) {
+ if (source == KEYBOARD_HARDWARE) {
+ keyboard->hardware_timestamp = SDL_GetTicks();
+ } else if (source == KEYBOARD_AUTORELEASE) {
keyboard->autorelease_pending = SDL_TRUE;
}
@@ -965,20 +969,25 @@ int SDL_SendKeyboardUnicodeKey(Uint32 ch)
if (mod & KMOD_SHIFT) {
/* If the character uses shift, press shift down */
- SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
+ SDL_SendKeyboardKeyInternal(KEYBOARD_VIRTUAL, SDL_PRESSED, SDL_SCANCODE_LSHIFT, SDLK_UNKNOWN);
}
/* Send a keydown and keyup for the character */
- SDL_SendKeyboardKey(SDL_PRESSED, code);
- SDL_SendKeyboardKey(SDL_RELEASED, code);
+ SDL_SendKeyboardKeyInternal(KEYBOARD_VIRTUAL, SDL_PRESSED, code, SDLK_UNKNOWN);
+ SDL_SendKeyboardKeyInternal(KEYBOARD_VIRTUAL, SDL_RELEASED, code, SDLK_UNKNOWN);
if (mod & KMOD_SHIFT) {
/* If the character uses shift, release shift */
- SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
+ SDL_SendKeyboardKeyInternal(KEYBOARD_VIRTUAL, SDL_RELEASED, SDL_SCANCODE_LSHIFT, SDLK_UNKNOWN);
}
return 0;
}
+int SDL_SendVirtualKeyboardKey(Uint8 state, SDL_Scancode scancode)
+{
+ return SDL_SendKeyboardKeyInternal(KEYBOARD_VIRTUAL, state, scancode, SDLK_UNKNOWN);
+}
+
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
{
return SDL_SendKeyboardKeyInternal(KEYBOARD_HARDWARE, state, scancode, SDLK_UNKNOWN);
@@ -1007,6 +1016,13 @@ void SDL_ReleaseAutoReleaseKeys(void)
}
keyboard->autorelease_pending = SDL_FALSE;
}
+
+ if (keyboard->hardware_timestamp) {
+ /* Keep hardware keyboard "active" for 250 ms */
+ if (SDL_TICKS_PASSED(SDL_GetTicks(), keyboard->hardware_timestamp + 250)) {
+ keyboard->hardware_timestamp = 0;
+ }
+ }
}
SDL_bool SDL_HardwareKeyboardKeyPressed(void)
@@ -1019,7 +1035,8 @@ SDL_bool SDL_HardwareKeyboardKeyPressed(void)
return SDL_TRUE;
}
}
- return SDL_FALSE;
+
+ return keyboard->hardware_timestamp ? SDL_TRUE : SDL_FALSE;
}
int SDL_SendKeyboardText(const char *text)
diff --git a/src/events/SDL_keyboard_c.h b/src/events/SDL_keyboard_c.h
index f234e8e..16a5fad 100644
--- a/src/events/SDL_keyboard_c.h
+++ b/src/events/SDL_keyboard_c.h
@@ -52,6 +52,9 @@ extern void SDL_SetKeyboardFocus(SDL_Window *window);
*/
extern int SDL_SendKeyboardUnicodeKey(Uint32 ch);
+/* Send a key from a virtual key source, like an on-screen keyboard */
+extern int SDL_SendVirtualKeyboardKey(Uint8 state, SDL_Scancode scancode);
+
/* Send a keyboard key event */
extern int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode);
extern int SDL_SendKeyboardKeyAutoRelease(SDL_Scancode scancode);
diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m
index 604de7d..ba49d6c 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.m
+++ b/src/video/uikit/SDL_uikitviewcontroller.m
@@ -467,8 +467,8 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
size_t deleteLength = SDL_utf8strlen([[committedText substringFromIndex:matchLength] UTF8String]);
while (deleteLength > 0) {
/* Send distinct down and up events for each backspace action */
- SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
- SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
+ SDL_SendVirtualKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
+ SDL_SendVirtualKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
--deleteLength;
}
}