Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion failure on MacOS when pressing globe key #2872

Closed
mgax opened this issue Jun 11, 2023 · 12 comments · Fixed by #3157
Closed

Assertion failure on MacOS when pressing globe key #2872

mgax opened this issue Jun 11, 2023 · 12 comments · Fixed by #3157

Comments

@mgax
Copy link

mgax commented Jun 11, 2023

On MacOS, pressing 🌐 in a winit window triggers an error message:

% cargo run --example=window >/dev/null
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/examples/window`
2023-06-11 21:19:26.092 window[92495:3007311] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
2023-06-11 21:19:26.190 window[92495:3007311] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
@madsmtm
Copy link
Member

madsmtm commented Jul 6, 2023

This is likely from this part of winit's code.

Not sure why it would throw that assertion though? Could you try with eprintln!("{:?}", ns_event); before that call?

Also, what MacOS version are you using?

@madsmtm
Copy link
Member

madsmtm commented Jul 6, 2023

I've hit the error myself a few times now, not sure exactly what causes it, but I think it is when flagsChanged: is called

@kchibisov kchibisov added this to the Version 0.29.0 milestone Jul 11, 2023
@mgax
Copy link
Author

mgax commented Jul 13, 2023

Currently running MacOS 13.4.1 on M1, winit master (c5941d1). I've added a couple of print statements and it seems the function bails when calling ns_event.charactersIgnoringModifiers():

diff --git a/src/platform_impl/macos/event.rs b/src/platform_impl/macos/event.rs
index 785647e3..5a50cae3 100644
--- a/src/platform_impl/macos/event.rs
+++ b/src/platform_impl/macos/event.rs
@@ -113,8 +113,10 @@ pub fn get_modifierless_char(scancode: u16) -> Key {
 }
 
 fn get_logical_key_char(ns_event: &NSEvent, modifierless_chars: &str) -> Key {
-    let string = ns_event
-        .charactersIgnoringModifiers()
+    eprintln!("get_logical_key_char?: {:?}", ns_event);
+    let t = ns_event.charactersIgnoringModifiers();
+    eprintln!("get_logical_key_char in the middle");
+    let string = t
         .map(|s| s.to_string())
         .unwrap_or_else(String::new);
     if string.is_empty() {
% cargo run --example=window >/dev/null
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/examples/window`
get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(-560.422,-150.617) time=94696.0 flags=0x800100 win=0x12184c240 winNum=3735 ctxt=0x0 keyCode=63 }
2023-07-13 11:17:13.691 window[55081:1023564] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(-563.398,-143.344) time=94696.2 flags=0x100 win=0x12184c240 winNum=3735 ctxt=0x0 keyCode=63 }
2023-07-13 11:17:13.811 window[55081:1023564] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

Indeed, the docs for charactersIgnoringModifiers say:

Raises an NSInternalInconsistencyException if accessed on a non-key event.

@kchibisov
Copy link
Member

Given that you have a log around already, could you press Shift, Command, etc, and then a Globe key?

@mgax
Copy link
Author

mgax commented Jul 13, 2023

Sure thing.

🌐 + no modifiers:

get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101130.9 flags=0x800100 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:28.661 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101131.0 flags=0x100 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:28.766 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

🌐 + Shift:

get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101137.4 flags=0x820102 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:35.109 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101137.5 flags=0x20102 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:35.265 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

🌐 + Command:

get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101139.3 flags=0x900108 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:37.060 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101139.5 flags=0x100108 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:37.211 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

🌐 + Option:

get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101140.7 flags=0x880120 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:38.441 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101140.8 flags=0x80120 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:38.545 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

🌐 + Control:

get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101141.9 flags=0x840101 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:39.639 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699
get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101142.0 flags=0x40101 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:39.729 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

@kchibisov
Copy link
Member

I was interested more in pressing them without a globe key. If you have such an option.

@mgax
Copy link
Author

mgax commented Jul 13, 2023

Ah, ok. I've attempted to filter out noisy messages, hopefully I haven't excluded anything important.

% cargo run --example=window | grep -v WaitCancelled | grep -v EventsCleared | grep -v RedrawRequested
[...]
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: ShiftLeft, logical_key: Shift, text: None, location: Left, state: Pressed, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Shift } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(SHIFT), pressed_mods: ModifiersKeys(LSHIFT) }) }
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: ShiftLeft, logical_key: Shift, text: None, location: Left, state: Released, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Shift } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(0x0), pressed_mods: ModifiersKeys(0x0) }) }
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: ControlLeft, logical_key: Control, text: None, location: Left, state: Pressed, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Control } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(CONTROL), pressed_mods: ModifiersKeys(LCONTROL) }) }
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: ControlLeft, logical_key: Control, text: None, location: Left, state: Released, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Control } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(0x0), pressed_mods: ModifiersKeys(0x0) }) }
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: AltLeft, logical_key: Alt, text: None, location: Left, state: Pressed, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Alt } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(ALT), pressed_mods: ModifiersKeys(LALT) }) }
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: AltLeft, logical_key: Alt, text: None, location: Left, state: Released, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Alt } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(0x0), pressed_mods: ModifiersKeys(0x0) }) }
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: SuperLeft, logical_key: Super, text: None, location: Left, state: Pressed, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Super } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(SUPER), pressed_mods: ModifiersKeys(LSUPER) }) }
TRACE [winit::platform_impl::platform::view] Triggered `flagsChanged:`
TRACE [winit::platform_impl::platform::view] Completed `flagsChanged:`
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: KeyboardInput { device_id: DeviceId(DeviceId), event: KeyEvent { physical_key: SuperLeft, logical_key: Super, text: None, location: Left, state: Released, repeat: false, platform_specific: KeyEventExtra { text_with_all_modifiers: None, key_without_modifiers: Super } }, is_synthetic: false } }
WindowEvent { window_id: WindowId(WindowId(4931775232)), event: ModifiersChanged(Modifiers { state: ModifiersState(0x0), pressed_mods: ModifiersKeys(0x0) }) }
[...]

@kchibisov
Copy link
Member

I was hoping for messages like

get_logical_key_char?: NSEvent { __inner: NSEvent: type=FlagsChanged loc=(92.2578,141.977) time=101141.9 flags=0x840101 win=0x12a806380 winNum=4092 ctxt=0x0 keyCode=63 }
2023-07-13 13:04:39.639 window[64600:1133348] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

But I can look myself once I have time. I'd at least

@mgax
Copy link
Author

mgax commented Jul 13, 2023

I was hoping for messages like get_logical_key_char?: [...]

get_logical_key_char is not getting called when pressing just shift, command, option or control. It also doesn't get called when pressing a regular key (c, v, space). It does get called when pressing ctrl-something. Here is the output for ctrl-a followed by ctrl-c with the patch from #2872 (comment):

get_logical_key_char?: NSEvent { __inner: NSEvent: type=KeyDown loc=(105.836,138.117) time=120780.7 flags=0x42100 win=0x122005a80 winNum=4977 ctxt=0x0 chars="" unmodchars="c" repeat=0 keyCode=8 }
get_logical_key_char in the middle
get_logical_key_char?: NSEvent { __inner: NSEvent: type=KeyUp loc=(105.836,138.117) time=120780.8 flags=0x42100 win=0x122005a80 winNum=4977 ctxt=0x0 chars="" unmodchars="c" repeat=0 keyCode=8 }
get_logical_key_char in the middle
get_logical_key_char?: NSEvent { __inner: NSEvent: type=KeyDown loc=(105.836,138.117) time=120781.3 flags=0x42100 win=0x122005a80 winNum=4977 ctxt=0x0 chars="" unmodchars="a" repeat=0 keyCode=0 }
get_logical_key_char in the middle
get_logical_key_char?: NSEvent { __inner: NSEvent: type=KeyUp loc=(105.836,138.117) time=120781.4 flags=0x42100 win=0x122005a80 winNum=4977 ctxt=0x0 chars="" unmodchars="a" repeat=0 keyCode=0 }
get_logical_key_char in the middle

@kchibisov
Copy link
Member

@madsmtm do you have an idea what is happening here? I guess we should sort of check event type before calling other funcs in update modifiers?

@kylewillmon
Copy link

I am hitting this same issue on macOS Ventura when using Command+Tab to change focus to the window. If the Command key is held for about a second after releasing this Tab key, the assertion failure occurs.

Using b2b4564 with the patch above, this is the output:

> cargo run --example window >/dev/null
   Compiling winit v0.29.1-beta (/Users/kyle/code/winit)
    Finished dev [unoptimized + debuginfo] target(s) in 1.11s
     Running `target/debug/examples/window`
get_logical_key_char?: NSEvent { __superclass: NSEvent: type=FlagsChanged loc=(-353.645,-256.742) time=97216.4 flags=0x100 win=0x125f094c0 winNum=3319 ctxt=0x0 keyCode=0 }
2023-09-29 10:25:17.235 window[18414:982887] *** Assertion failure in -[NSEvent charactersIgnoringModifiers], NSEvent.m:2699

To be clear, here are the steps:

  1. Change focus away from the window example
  2. Hold Command and press Tab to switch focus back to the window example
  3. Continue holding Command for an extra second
  4. Release Command

@kchibisov
Copy link
Member

I think what we should be doing, is not to try get the characters when the flag is being pressed? it's not like flag could produce the character input. I'll try to repro that once I want to work on macOS stuff.

kchibisov added a commit to kchibisov/winit that referenced this issue Oct 17, 2023
Sometimes FlagsChanged events don't carry any KeyCode information, thus
we can't create a synthetic presses events for them.

However in such cases, modifiers information is still accurate, thus
propagate it.

Fixes rust-windowing#2872.
kchibisov added a commit that referenced this issue Oct 17, 2023
Sometimes FlagsChanged events don't carry any KeyCode information, thus
we can't create a synthetic presses events for them.

However in such cases, modifiers information is still accurate, thus
propagate it.

Fixes #2872.
kchibisov added a commit to kchibisov/winit that referenced this issue Oct 17, 2023
Sometimes FlagsChanged events don't carry any KeyCode information, thus
we can't create a synthetic presses events for them.

However in such cases, modifiers information is still accurate, thus
propagate it.

Fixes rust-windowing#2872.
kchibisov added a commit that referenced this issue Oct 21, 2023
Sometimes FlagsChanged events don't carry any KeyCode information, thus
we can't create a synthetic presses events for them.

However in such cases, modifiers information is still accurate, thus
propagate it.

Fixes #2872.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

4 participants