Skip to content

Commit

Permalink
macOS: Remove is_key_down from ViewState
Browse files Browse the repository at this point in the history
Fixes rust-windowing#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` didnt 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`.
  • Loading branch information
jayache80 committed Jul 11, 2021
1 parent 5a65347 commit f6ad9d7
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ pub(super) struct ViewState {
pub cursor_state: Arc<Mutex<CursorState>>,
ime_spot: Option<(f64, f64)>,
raw_characters: Option<String>,
is_key_down: bool,
pub(super) modifiers: ModifiersState,
tracking_rect: Option<NSInteger>,
}
Expand All @@ -74,7 +73,6 @@ pub fn new_view(ns_window: id) -> (IdRef, Weak<Mutex<CursorState>>) {
cursor_state,
ime_spot: None,
raw_characters: None,
is_key_down: false,
modifiers: Default::default(),
tracking_rect: None,
};
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit f6ad9d7

Please sign in to comment.