diff --git a/app/src/hid_keyboard.c b/app/src/hid_keyboard.c index 3ac1a441..2809ccd6 100644 --- a/app/src/hid_keyboard.c +++ b/app/src/hid_keyboard.c @@ -278,7 +278,8 @@ push_mod_lock_state(struct sc_hid_keyboard *kb, uint16_t sdl_mod) { static void sc_key_processor_process_key(struct sc_key_processor *kp, - const SDL_KeyboardEvent *event) { + const SDL_KeyboardEvent *event, + bool device_clipboard_set) { if (event->repeat) { // In USB HID protocol, key repeat is handled by the host (Android), so // just ignore key repeat here. @@ -298,11 +299,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp, } } - SDL_Keycode keycode = event->keysym.sym; - bool down = event->type == SDL_KEYDOWN; - bool ctrl = event->keysym.mod & KMOD_CTRL; - bool shift = event->keysym.mod & KMOD_SHIFT; - if (ctrl && !shift && keycode == SDLK_v && down) { + if (device_clipboard_set) { // Ctrl+v is pressed, so clipboard synchronization has been // requested. Wait a bit so that the clipboard is set before // injecting Ctrl+v via HID, otherwise it would paste the old diff --git a/app/src/input_manager.c b/app/src/input_manager.c index c15e0427..16c82234 100644 --- a/app/src/input_manager.c +++ b/app/src/input_manager.c @@ -510,7 +510,8 @@ input_manager_process_key(struct input_manager *im, return; } - if (ctrl && !shift && keycode == SDLK_v && down && !repeat) { + bool is_ctrl_v = ctrl && !shift && keycode == SDLK_v && down && !repeat; + if (is_ctrl_v) { if (im->legacy_paste) { // inject the text as input events clipboard_paste(controller); @@ -525,7 +526,7 @@ input_manager_process_key(struct input_manager *im, } } - im->kp->ops->process_key(im->kp, event); + im->kp->ops->process_key(im->kp, event, is_ctrl_v); } static void diff --git a/app/src/keyboard_inject.c b/app/src/keyboard_inject.c index bcc85da8..e59b5520 100644 --- a/app/src/keyboard_inject.c +++ b/app/src/keyboard_inject.c @@ -188,7 +188,13 @@ convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to, static void sc_key_processor_process_key(struct sc_key_processor *kp, - const SDL_KeyboardEvent *event) { + const SDL_KeyboardEvent *event, + bool device_clipboard_set) { + // The device clipboard synchronization and the key event messages are + // serialized, there is nothing special to do to ensure that the clipboard + // is set before injecting Ctrl+v. + (void) device_clipboard_set; + struct sc_keyboard_inject *ki = DOWNCAST(kp); if (event->repeat) { diff --git a/app/src/trait/key_processor.h b/app/src/trait/key_processor.h index 5790310b..1f8132f2 100644 --- a/app/src/trait/key_processor.h +++ b/app/src/trait/key_processor.h @@ -18,8 +18,17 @@ struct sc_key_processor { }; struct sc_key_processor_ops { + + /** + * Process the keyboard event + * + * The flag `device_clipboard_set` indicates that the input manager sent a + * control message to synchronize the device clipboard as a result of this + * key event. + */ void - (*process_key)(struct sc_key_processor *kp, const SDL_KeyboardEvent *event); + (*process_key)(struct sc_key_processor *kp, const SDL_KeyboardEvent *event, + bool device_clipboard_set); void (*process_text)(struct sc_key_processor *kp,