Repository navigation
Post simulated key events with HID system state to keep input state intact - #1370
Conversation
…ntact Key events created without an event source carry no HID system state, a zero timestamp and no keyboard type. Some event consumers treat such events as foreign and mistrack input state around them - e.g. Wine applications drop a held-button mouse capture when a key press arrives, and Wine's modifier tracking leaves Control stuck after a space-switch symbolic hotkey (Control+Arrow) fires mid focus change, turning later game input into Control-modified keys. Create simulated events (both KeySimulator key presses and symbolic hotkey sequences) from a hidSystemState source and stamp a real timestamp and keyboard type so they are indistinguishable from hardware key presses. Covered by a KeySimulator test recording posted events and asserting the HID system state, timestamp and keyboard type fields.
|
Thanks! One concern is that Apple documents One possible direction would be to introduce a small shared keyboard-type snapshot in |
…ntact Key events created without an event source carry no HID system state, a zero timestamp and a default keyboard type. Some event consumers treat such events as foreign and mistrack input state around them - e.g. Wine applications drop a held-button mouse capture when a key press arrives, and leave Control stuck after a Control+Arrow space-switch hotkey fires mid focus change. Create simulated events (KeySimulator key presses and symbolic hotkey sequences) through a shared CGEvent.makeHardwareLikeKeyEvent helper that uses a hidSystemState source, stamps a real timestamp and sets the keyboard type so they are indistinguishable from hardware key presses. The keyboard type is read from the event source instead of LMGetKbdType(), which is not thread-safe and is already used on the main thread by KeyCodeResolver while the helper runs on the event thread. A per-event source needs no shared state and is always fresh. Covered by a KeySimulator test in the KeyKit package (`swift test` in Modules/KeyKit; not part of the Xcode scheme).
|
Thanks, good catch. Resolved by removing the Each simulated event is now created from its own I went this way instead of the snapshot for three reasons. There is no shared mutable state, so nothing for the Creating a source per event is cheap at key-press rates. One caveat found while testing: |
|
Looks good to me. Thanks for the update! |
This is the follow-up fix. Similar situation to #1369.
In this case CrossOver + MX Master 4. Side wheel remapped to keyboard buttons F1/F2. If hold RBM and spin side wheel for triggering F1/F2, RBM still in hold state but no action until release and press it again.
Summary
Key events created without an event source carry no HID system state, a zero timestamp and no keyboard type. Some event consumers treat such events as foreign and mistrack input state around them - e.g. Wine applications drop a held-button mouse capture when a key press arrives, and Wine's modifier tracking leaves Control stuck after a space-switch symbolic hotkey (Control+Arrow) fires mid focus change, turning later game input into Control-modified keys. Create simulated events (both KeySimulator key presses and symbolic hotkey sequences) from a hidSystemState source and stamp a real timestamp and keyboard type so they are indistinguishable from hardware key presses.
Covered by a KeySimulator test recording posted events and asserting the HID system state, timestamp and keyboard type fields.