Skip to content

Clear modifier flags on synthetic Enter on macOS#131

Merged
maraf merged 1 commit into
mainfrom
maraf/fix-macos-enter-modifier-leak
Apr 23, 2026
Merged

Clear modifier flags on synthetic Enter on macOS#131
maraf merged 1 commit into
mainfrom
maraf/fix-macos-enter-modifier-leak

Conversation

@maraf

@maraf maraf commented Apr 23, 2026

Copy link
Copy Markdown
Member

When auto-submitting a snippet with Cmd+Enter on macOS, the target app (e.g., Edge) would often see Cmd+Enter instead of plain Enter — for example, pasting a URL snippet into the address bar would open a new tab rather than just navigate in the current one.

Cause

MacOSApplication.SendEnterShortcut created the Return CGEvent without ever calling CGEventSetFlags. The event source (kCGEventSourceStateHIDSystemState) inherits the current modifier state, and the preceding synthetic Cmd+V posted both keyDown and keyUp for V with the Cmd flag set, which leaves Cmd latched in the session state. The Return event then went out carrying Cmd.

Fix

Explicitly set flags = 0 on both the keyDown and keyUp Return events in SendEnterShortcut, so the synthetic Enter is delivered as a plain Enter regardless of any stale Cmd state from the paste or a physically still-held Cmd key.

Note: I also considered re-awaiting WaitForAutoSubmitModifierReleaseAsync() right before SimulateEnter, but that ended up waiting indefinitely (or until the user physically tapped Cmd again) because our own Cmd+V injection keeps Cmd reported as pressed in the combined session state. Clearing the flags on the Return event itself is the right layer to solve this.

When auto-submitting a snippet with Cmd+Enter, the preceding synthetic
Cmd+V events left Cmd latched in the HID session state, so the Return
event that followed inherited the Cmd flag. Targets like Edge then saw
Cmd+Enter and opened a new tab instead of just submitting.

Explicitly set flags to 0 on both the keyDown and keyUp Return events
so the synthetic Enter is delivered as plain Enter regardless of any
stale or still-held Cmd state.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes macOS auto-submit behavior so that the synthetic “Enter” sent after a Cmd+V paste is delivered as a plain Return keypress (not Cmd+Enter), preventing target apps (e.g., Edge) from treating it as a command-modified action.

Changes:

  • Clear CoreGraphics modifier flags on the synthetic Return keyDown/keyUp events in MacOSApplication.SendEnterShortcut.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@maraf maraf merged commit ee0e21b into main Apr 23, 2026
5 checks passed
@maraf maraf deleted the maraf/fix-macos-enter-modifier-leak branch April 23, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants