input-inject: wheel remainder, release buttons at the real pointer

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 02:52:40 -07:00
1 parent f6fc2c5bd3
commit ccb058ed3a
2 files changed
+37 -12

No files matched your search

+29 -10
View File
@@ -56,6 +56,8 @@ public final class Injector: @unchecked Sendable {
private var state = KeyState()
private var clicks: ClickTracker
private var lastPosted: CGPoint?
private var verticalWheelRemainder: Int32 = 0
private var horizontalWheelRemainder: Int32 = 0
private var dropped: UInt64 = 0
/// An injector whose events are tagged so our own tap passes them through
@@ -278,11 +280,15 @@ public final class Injector: @unchecked Sendable {
}
/// `MOUSEEVENTF_WHEEL` / `HWHEEL` (`InputSimulation.cs:204-209`). Windows
/// counts in 120ths of a notch and macOS counts lines, so the packet's
/// `WheelDelta` divides straight down (docs/protocol.md §5.2).
/// counts in 120ths of a notch and macOS counts lines (docs/protocol.md
/// §5.2), so 120 units become one line.
///
/// A high-resolution wheel sends fractions of a notch, which Windows
/// accumulates inside `SendInput`; here the fraction is carried in a
/// remainder so a slow scroll is not silently thrown away.
private func postWheel(vertical: Int32, horizontal: Int32, at point: CGPoint) {
let lines = vertical / WindowsMessage.wheelDelta
let columns = horizontal / WindowsMessage.wheelDelta
let lines = Injector.notches(vertical, remainder: &verticalWheelRemainder)
let columns = Injector.notches(horizontal, remainder: &horizontalWheelRemainder)
guard lines != 0 || columns != 0 else { return }
guard
let event = CGEvent(
@@ -315,11 +321,11 @@ public final class Injector: @unchecked Sendable {
}
postKey(keycode: keycode, vk: vk, down: false)
}
for button in state.buttonReleaseOrder() {
let buttons = state.buttonReleaseOrder()
let at = buttons.isEmpty ? CGPoint(x: 0, y: 0) : (currentPointer() ?? CGPoint(x: 0, y: 0))
for button in buttons {
_ = state.buttonUp(button.number)
postButtonEvent(
button.number, down: false, at: lastPosted ?? CGPoint(x: 0, y: 0),
clickState: button.clickState)
postButtonEvent(button.number, down: false, at: at, clickState: button.clickState)
}
clicks.reset()
// `ResetSystemKeyFlags` (`InputSimulation.cs:477-480`). Windows leaves the
@@ -367,11 +373,24 @@ public final class Injector: @unchecked Sendable {
// MARK: - Helpers
/// Whole lines out of a stream of 120ths, keeping the fraction for next time.
private static func notches(_ delta: Int32, remainder: inout Int32) -> Int32 {
remainder &+= delta
let whole = remainder / WindowsMessage.wheelDelta
remainder &-= whole &* WindowsMessage.wheelDelta
return whole
}
/// The pointer right now. Windows reads it with `GetCursorPos` on the same
/// paths (`Receiver.cs:149, 156`).
private func pointer(layout: ScreenLayout) -> CGPoint {
if let here = CGEvent(source: nil)?.location { return here }
return lastPosted ?? Coordinates.centre(of: layout.primary)
currentPointer() ?? Coordinates.centre(of: layout.primary)
}
/// The pointer right now, for the paths that have no `ScreenLayout` to fall
/// back on — `releaseAll()` is called from recovery, not from a packet.
private func currentPointer() -> CGPoint? {
CGEvent(source: nil)?.location ?? lastPosted
}
/// Keeps an injected point on a real display. Windows relies on `SendInput`
+8 -2
View File
@@ -67,7 +67,10 @@ work log.
`mouseEventClickState` 2, 3…, reset otherwise.
- **Wheel**: `WM_MOUSEWHEEL` → `CGEvent(scrollWheelEvent2Source:units:
.line, wheelCount: 1, wheel1: WheelDelta / 120)`; `WM_MOUSEHWHEEL` →
`wheelCount: 2, wheel2:`. Sign: Windows +120 is away from the user,
`wheelCount: 2, wheel2:`. ***(amended)*** The division keeps a
remainder per axis: a high-resolution wheel sends fractions of a notch
and integer division alone would drop a slow scroll entirely. Sign:
Windows +120 is away from the user,
which is CG line +1. The user's "natural scrolling" preference is
applied by the system to posted events too; whether that matches what
they expect from a remote mouse is a CP1 question, recorded in §10.5.
@@ -313,7 +316,10 @@ the reasoning here).
the function keys — real macOS events carry them and some text views
read them. Windows has no equivalent; this is the mirror image of
input-capture synthesising `LLKHF_EXTENDED` (§10.4 note 6).
9. **Two CP1 questions recorded, not decided**: remote ⌘L locks this Mac
9. **Wheel deltas keep a per-axis remainder**, mirroring what
`EventTap` does in the other direction, so a high-resolution wheel's
sub-notch deltas are accumulated rather than truncated to nothing.
10. **Two CP1 questions recorded, not decided**: remote ⌘L locks this Mac
(faithful to Win+L with D4's Win=⌘, but ⌘L is a common Mac shortcut),
and whether "natural scrolling" should apply to a remote wheel.