Skip to content

fix(mouse-enhancer): middle-click stops working after app restart - #205

Draft
ayangweb wants to merge 1 commit into
ggbond268:mainfrom
ayangweb:main
Draft

fix(mouse-enhancer): middle-click stops working after app restart#205
ayangweb wants to merge 1 commit into
ggbond268:mainfrom
ayangweb:main

Conversation

@ayangweb

@ayangweb ayangweb commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

fixed #202

After each app or system restart, the simulate-middle-click feature is silently broken until the user toggles its switch off and back on.

Two defects in MouseEnhancerMiddleClickSession:

1. CGEvent tap not re-enabled when macOS auto-disables it

macOS can send tapDisabledByTimeout / tapDisabledByUserInput to a non-listen cghidEventTap during busy startup or after sleep. The tap callback did not handle these events, so the tap stayed disabled permanently. MouseEnhancerSession (scroll reversing) already has this recovery; add the same to the middle-click session:

if type == .tapDisabledByTimeout || type == .tapDisabledByUserInput {
    if let tap = session.eventTap, CFMachPortIsValid(tap) {
        CGEvent.tapEnable(tap: tap, enable: true)
    }
    return passthrough
}

2. MTDeviceCreateList returns empty at login-item startup

When the app launches as a login item at boot, MultitouchSupport.framework may not have finished enumerating devices yet, so _mtDeviceCreateList() returns an empty array and no touch listeners are registered. The existing IOKit kIOFirstMatchNotification recovery does not help here—it drains currently-present devices on registration (arming future arrivals) but never fires a callback for devices already in the IOKit registry. Result: no touch data, threeDown never set, middle-click silently dead.

Schedule a one-shot 3-second rebuild at the end of start() when no devices are found, mirroring the existing wake-restart recovery:

if devices.isEmpty {
    scheduleRestart(after: 3, reason: "noDevicesAtStart")
}

Tests

Added testMiddleClickStartsOnActivateWhenSavedAsEnabledAndAccessibilityGranted and testMiddleClickDoesNotStartOnActivateWhenAccessibilityDenied to cover the activate() path (app-restart scenario) with pre-persisted settings, which had no test coverage.

@ayangweb

Copy link
Copy Markdown
Collaborator Author

@ggbond268 copilot 修复的,需要你来测试

@ayangweb
ayangweb marked this pull request as draft July 19, 2026 04:35
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.

[bug] 模拟鼠标中键需要重启开关才生效

2 participants