Fix macOS modifier tracking to use absolute NSEvent state#184
Open
relh wants to merge 1 commit into
Open
Conversation
processFlagsChanged toggled each modifier key per event, so any edge delivered while the window was not key (focus stolen mid-chord, Cmd-Tab entry, activation settling during launch) latched an inverted modifier for the rest of the session. Every unmodified key chord then resolved with a phantom SHIFT/CTRL/ALT attached, silently retargeting exact-match consumers. Shift/Control/Option/Command now derive up/down from the absolute NSEventModifierFlags state (class bit plus IOKit device bits for the left/right split), so a missed edge self-corrects on the next flagsChanged. windowDidBecomeKey seeds the same absolute state after clearing buttons, so a modifier held across focus gain cannot register its release as a phantom press. CapsLock keeps toggle semantics. Also register the NSWindow key-eligibility override with its real zero-argument selector: 'canBecomeKeyWindow:' (with colon) never overrode the getter, leaving borderless windows unable to become key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
On macOS,
processFlagsChangedtracks Shift/Control/Option/Command bytoggling per event:
Any modifier transition that happens while the window is not key never
reaches this handler, and the next transition that does arrive gets
interpreted backwards. From that point the modifier is inverted in
window.buttonDownfor the rest of the session: windy reports Shift/Ctrldown whenever the key is physically up, and vice versa.
Minimal repro: hold Shift, click the windy window to focus it, release
Shift.
windowDidBecomeKeycleared the button state while Shift wasphysically down, so the release is delivered to the toggle as a press —
buttonDownnow contains a phantom Shift indefinitely, and everysubsequent Shift press/release stays inverted. The same thing happens via
Cmd-Tab with a modifier held, or when a window activates itself while the
user is typing elsewhere. Applications that do exact-modifier shortcut
matching on top of
buttonDownthen silently drop unmodified keys (aplain key press resolves as SHIFT-/CTRL-qualified), which is very hard to
diagnose from the app side.
The fix
NSEventTypeFlagsChangedevents carry the absolute modifier state inmodifierFlags, so the handler no longer needs to infer edges at all:every
flagsChanged— the class bit answers "any key of this modifierdown", and the IOKit device-dependent bits pick the left/right side.
A missed edge now self-corrects on the next event instead of latching.
If a device omits the L/R bits (some virtual keyboards), the changed
keycode is used as a fallback without disturbing its sibling.
windowDidBecomeKeyseeds the same absolute state (class propertyNSEvent.modifierFlags) right afterclearButtonsTemplate, so amodifier held across the focus gain is tracked as down and its upcoming
release cannot register as a phantom press.
Also:
canBecomeKeyWindowselector registrationWindyWindowregistersaddMethod "canBecomeKeyWindow:", ...— with atrailing colon — but the NSWindow key-eligibility check is the
zero-argument getter
canBecomeKeyWindow. The override therefore neverinstalls, and since the AppKit default for borderless windows is
NO,undecorated windy windows can never become key and never receive keyboard
events. Registered with the correct colon-less selector (and the handler
signature reduced to
(self, cmd)to match).Validation
nim check src/windy.nimpasses on macOS. Manually reasoned through thestate table for held-across-focus, missed-release, both-sides-held, and
virtual-keyboard (no device bits) cases; the absolute mask makes every
path converge to physical truth on the next
flagsChanged.