input-inject: modifiers post as flagsChanged, monotonic click clock, move deltas

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 02:48:32 -07:00
1 parent e0123a1745
commit d48f644450
2 files changed
+29 -5

No files matched your search

+23 -5
View File
@@ -157,7 +157,16 @@ public final class Injector: @unchecked Sendable {
else { return }
event.flags = CGEventFlags(
rawValue: state.eventFlags | KeyState.positionalFlags(forKeycode: keycode))
if isRepeat { event.setIntegerValueField(.keyboardEventAutorepeat, value: 1) }
if KeyState.reportsAsFlagsChanged(vk: vk) {
// macOS reports a modifier as a state change, not as a key press: an
// ordinary key event for keycode 56 is not how anything learns that
// Shift went down. Windows has no such split — its hook and
// `SendInput` both speak in `VK_LSHIFT` down/up — so the event is
// built as a key event and re-typed here.
event.type = .flagsChanged
} else if isRepeat {
event.setIntegerValueField(.keyboardEventAutorepeat, value: 1)
}
event.post(tap: .cghidEventTap)
}
@@ -208,9 +217,11 @@ public final class Injector: @unchecked Sendable {
mouseButton: Injector.cgButton(button ?? 0))
else { return }
if let button { event.setIntegerValueField(.mouseEventButtonNumber, value: button) }
// Games and anything using `NSEvent.deltaX` read the delta rather than
// the position; `SendInput` fills it in on Windows, `CGEvent` does not.
let previous = lastPosted ?? target
event.setIntegerValueField(.mouseEventDeltaX, value: Int64(target.x - previous.x))
event.setIntegerValueField(.mouseEventDeltaY, value: Int64(target.y - previous.y))
event.setIntegerValueField(.mouseEventDeltaX, value: Injector.whole(target.x - previous.x))
event.setIntegerValueField(.mouseEventDeltaY, value: Injector.whole(target.y - previous.y))
event.flags = CGEventFlags(rawValue: state.eventFlags)
event.post(tap: .cghidEventTap)
lastPosted = target
@@ -242,8 +253,9 @@ public final class Injector: @unchecked Sendable {
private func postButton(_ number: Int64, down: Bool, at point: CGPoint) {
let clickState: Int64
if down {
clickState = clicks.click(
button: number, at: point, now: Date().timeIntervalSinceReferenceDate)
// `EventTap.now()` is the lane's monotonic clock; a wall clock would
// turn an NTP step into a spurious double click.
clickState = clicks.click(button: number, at: point, now: EventTap.now())
state.buttonDown(number, clickState: clickState)
} else {
clickState = state.buttonUp(number)
@@ -400,6 +412,12 @@ public final class Injector: @unchecked Sendable {
}
}
/// A `CGFloat` as whole pixels, never trapping on a value a broken display
/// list could produce (CLAUDE.md: no traps in production paths).
private static func whole(_ value: CGFloat) -> Int64 {
Int64(exactly: value.rounded(.towardZero)) ?? 0
}
private static func systemDoubleClickInterval() -> TimeInterval {
let interval = NSEvent.doubleClickInterval
return interval > 0 ? interval : 0.5
+6
View File
@@ -76,6 +76,12 @@ public struct KeyState: Sendable {
KeyMap.leftMenu, KeyMap.rightMenu, KeyMap.leftShift, KeyMap.rightShift,
]
/// Whether macOS reports this key as a `flagsChanged` event rather than a
/// key press — the eight modifiers and Caps Lock.
public static func reportsAsFlagsChanged(vk: Int32) -> Bool {
vk == KeyMap.capital || modifierVKs.contains(vk)
}
/// The side-less `VK_SHIFT`/`VK_CONTROL`/`VK_MENU` mapped onto the left key,
/// everything else unchanged.
///