Skip to content

fix: retry re-enabling event tap after wake if it fails - #5

Open
lovestorms wants to merge 1 commit into
maclifevn:mainfrom
lovestorms:fix/eventtap-wake-retry
Open

lovestorms wants to merge 1 commit into
maclifevn:mainfrom
lovestorms:fix/eventtap-wake-retry

Conversation

@lovestorms

Copy link
Copy Markdown

Problem

After the Mac wakes from sleep (or sometimes after locking/unlocking the screen), MKey can stop typing Vietnamese diacritics entirely. The only reliable fix without this patch is quitting and relaunching the app.

Root cause

receiveWake() in MkeyApp.swift called MKBridge.startEventTap() exactly once and silently dropped the return value:

@objc private func receiveWake(_ note: Notification) {
    _ = MKBridge.startEventTap()
}

Right after wake, AXIsProcessTrusted() can report true while macOS hasn't fully reinstated the underlying Accessibility/event-tap permission yet, so CGEventTapCreate inside startEventTap fails and returns NO. Unlike startEngine() (used at launch), which checks the return value and falls back to askForAccessibility(), receiveWake() had no retry path — so the event tap was simply never recreated until the app was relaunched.

This matches symptoms reported upstream against the shared OpenKey engine:

Fix

Retry startEventTap() a few times (0.5s apart, up to 5 attempts) instead of giving up after the first failure on wake:

@objc private func receiveWake(_ note: Notification) {
    wakeRetryCount = 0
    retryStartEventTapAfterWake()
}

private func retryStartEventTapAfterWake() {
    if MKBridge.startEventTap() {
        AppState.shared.accessibilityGranted = true
        return
    }
    guard wakeRetryCount < 5 else { return }
    wakeRetryCount += 1
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
        self?.retryStartEventTapAfterWake()
    }
}

Testing

  • xcodegen generate + xcodebuild -project mkey.xcodeproj -scheme mkey -configuration Debug build → BUILD SUCCEEDED.
  • Haven't been able to reliably reproduce the intermittent wake-time tap-creation failure to verify the retry fires in practice (it's reported as ~1 in 5 sleep/wake cycles upstream), so please treat this as a best-effort fix based on the code path analysis above rather than a confirmed repro-then-fix. Happy to adjust the retry count/interval if you have more context on how often CGEventTapCreate actually fails here.

Fix by Duc Do

startEventTap() can fail right after the Mac wakes from sleep even
though AXIsProcessTrusted() reports true later, because macOS briefly
reinstates the Accessibility permission asynchronously. receiveWake()
used to call startEventTap() once and silently drop the failure, which
left Vietnamese typing dead until the app was relaunched. Retry a few
times with a short delay instead of giving up on the first failure.

Fix by Duc Do
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