From ef271584b45901bed43691880c26c85e8b882204 Mon Sep 17 00:00:00 2001 From: "prompt.ac/@jeffrey" Date: Fri, 14 Aug 2026 17:43:47 -0400 Subject: [PATCH] Cancel interrupted TrackDrum command gestures --- slab/menuband-trackpad/Sources/main.swift | 30 ++++++++++++++++++++++- slab/menuband-trackpad/verify-app.sh | 5 ++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/slab/menuband-trackpad/Sources/main.swift b/slab/menuband-trackpad/Sources/main.swift index e49e72bd0a..19d4a9476c 100644 --- a/slab/menuband-trackpad/Sources/main.swift +++ b/slab/menuband-trackpad/Sources/main.swift @@ -42,6 +42,29 @@ private final class TrackpadInteractionShield { private var commandExitArmAfter: CFTimeInterval = 0 private var lastCommandTap: CFTimeInterval? + private static let commandKeyCodes: Set = [54, 55] + + /// The permission-free watchdog only receives snapshots, not key events. + /// While the first Command tap is pending, sample the keyboard so any + /// intervening key turns `Command, key, Command` into a cancelled run. + private static func hasNonCommandKeyDown() -> Bool { + let flags = CGEventSource.flagsState(.combinedSessionState) + let otherModifiers: CGEventFlags = [ + .maskAlphaShift, .maskShift, .maskControl, + .maskAlternate, .maskSecondaryFn, + ] + if !flags.intersection(otherModifiers).isEmpty { return true } + + for raw in 0..<128 { + let key = CGKeyCode(raw) + guard !commandKeyCodes.contains(key) else { continue } + if CGEventSource.keyState(.combinedSessionState, key: key) { + return true + } + } + return false + } + var onExitRequested: (() -> Void)? var onCommandDoubleTap: (() -> Void)? @@ -101,13 +124,18 @@ private final class TrackpadInteractionShield { let escapePressed = escapeDown && !self.escapeWasDown var commandDoubleTapped = false let now = CACurrentMediaTime() + if self.lastCommandTap != nil, Self.hasNonCommandKeyDown() { + self.lastCommandTap = nil + } if !self.commandExitArmed { if !commandDown && now >= self.commandExitArmAfter { self.commandExitArmed = true self.lastCommandTap = nil } } else if commandDown && !self.commandWasDown { - if let prior = self.lastCommandTap, now - prior <= 0.75 { + if Self.hasNonCommandKeyDown() { + self.lastCommandTap = nil + } else if let prior = self.lastCommandTap, now - prior <= 0.75 { commandDoubleTapped = true self.lastCommandTap = nil } else { diff --git a/slab/menuband-trackpad/verify-app.sh b/slab/menuband-trackpad/verify-app.sh index 2a018f8569..3b602ad575 100755 --- a/slab/menuband-trackpad/verify-app.sh +++ b/slab/menuband-trackpad/verify-app.sh @@ -18,11 +18,12 @@ fi echo "helper must be headless (LSUIElement)" >&2; exit 1; } -if ! strings "${BIN}" | grep -q 'MultitouchSupport.framework'; then +BIN_STRINGS="$(strings "${BIN}")" +if ! grep -q 'MultitouchSupport.framework' <<<"${BIN_STRINGS}"; then echo "global trackpad implementation is missing" >&2 exit 1 fi -if strings "${BIN}" | grep -Eq 'AVAudioEngine|TracktrampMetalView|NSWindow'; then +if grep -Eq 'AVAudioEngine|TracktrampMetalView|NSWindow' <<<"${BIN_STRINGS}"; then echo "audio or standalone instrument UI leaked into helper" >&2 exit 1 fi -- 2.51.2