From 27e654834327ea775b9b75423bc3c5f318ddffd5 Mon Sep 17 00:00:00 2001 From: jim jammer <15721514+jayache80@users.noreply.github.com> Date: Tue, 13 Jul 2021 08:38:01 -0700 Subject: [PATCH] macOS: Remove `is_key_down` from `ViewState` (#1489) Fixes #1488. `is_key_down` was only set to true when `insertText` was called. Therefore `is_key_down` was actually meant to store whether the most recently pressed key generated an `insertText` event, at which, winit produces a `ReceivedCharacter`. The issue is that `insertText` is *not* called for "key repeat", but winit wants to send `ReceivedCharacter` events for "key repeat" too. To solve this, the `is_key_down` variable was then checked in the `key_down` function to determine whether it was valid to produce repeated `ReceivedCharacter` events during "key repeat". However this check is not needed since `ReceivedCharacter` must always be called if the key event has a non-empty `characters` property. Furthermore `is_key_down` didn't actually store whether the previously pressed character had an `insertText` event, because it was incorrectly set to false on every "key up". This meant that if two keys were pressed consecutively and then the first was released, then `is_key_down` was set to false even if the most recent keypress did actually produce an `insertText`. Update changelog. --- CHANGELOG.md | 1 + src/platform_impl/macos/view.rs | 7 +------ 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28ecbf83..ce16fb79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - **Breaking:** On Web, remove the `stdweb` backend. - Added `Window::focus_window`to bring the window to the front and set input focus. - On Wayland and X11, implement `is_maximized` method on `Window`. +- On macOS, fix issue where `ReceivedCharacter` was not being emitted during some key repeat events. # 0.25.0 (2021-05-15) diff --git a/src/platform_impl/macos/view.rs b/src/platform_impl/macos/view.rs index 360f289a..e44fd05b 100644 --- a/src/platform_impl/macos/view.rs +++ b/src/platform_impl/macos/view.rs @@ -55,7 +55,6 @@ pub(super) struct ViewState { pub cursor_state: Arc>, ime_spot: Option<(f64, f64)>, raw_characters: Option, - is_key_down: bool, pub(super) modifiers: ModifiersState, tracking_rect: Option, } @@ -74,7 +73,6 @@ pub fn new_view(ns_window: id) -> (IdRef, Weak>) { cursor_state, ime_spot: None, raw_characters: None, - is_key_down: false, modifiers: Default::default(), tracking_rect: None, }; @@ -516,7 +514,6 @@ extern "C" fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_ran let slice = slice::from_raw_parts(characters.UTF8String() as *const c_uchar, characters.len()); let string = str::from_utf8_unchecked(slice); - state.is_key_down = true; // We don't need this now, but it's here if that changes. //let event: id = msg_send![NSApp(), currentEvent]; @@ -675,7 +672,7 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) { let pass_along = { AppState::queue_event(EventWrapper::StaticEvent(window_event)); // Emit `ReceivedCharacter` for key repeats - if is_repeat && state.is_key_down { + if is_repeat { for character in characters.chars().filter(|c| !is_corporate_character(*c)) { AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent { window_id, @@ -705,8 +702,6 @@ extern "C" fn key_up(this: &Object, _sel: Sel, event: id) { let state_ptr: *mut c_void = *this.get_ivar("winitState"); let state = &mut *(state_ptr as *mut ViewState); - state.is_key_down = false; - let scancode = get_scancode(event) as u32; let virtual_keycode = retrieve_keycode(event);