hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
hop / Sources / DictationKeys.swift
| 1 | import AppKit |
| 2 | import Carbon.HIToolbox |
| 3 | |
| 4 | /// Watches the Fn (globe) key at the far-left corner of the keyboard: |
| 5 | /// hold Fn talk, release to type it |
| 6 | /// Fn then Space hands-free (Fn again to finish); a quick double tap of Fn does the same |
| 7 | /// Fn + Control a spoken instruction (rewrite the selection, or hand it to Hop's agent) |
| 8 | /// Esc cancel |
| 9 | /// Uses the same macOS permission as pasting. Keys are only looked at for these shortcuts; nothing is recorded. |
| 10 | @MainActor |
| 11 | final class DictationKeys { |
| 12 | private let d: Dictation |
| 13 | private var tap: CFMachPort? |
| 14 | private var source: CFRunLoopSource? |
| 15 | private var fnHeld = false, fnDownAt = 0.0, usedAsModifier = false, ignoreRelease = false |
| 16 | private var pendingTapAt = 0.0 |
| 17 | |
| 18 | init(_ d: Dictation) { self.d = d } |
| 19 | var running: Bool { tap != nil } |
| 20 | |
| 21 | /// Starts or stops watching to match the settings and permissions of this moment. |
| 22 | func refresh() { |
| 23 | if d.armed && AXIsProcessTrusted() { start() } else { stop() } |
| 24 | } |
| 25 | |
| 26 | private func start() { |
| 27 | guard tap == nil else { return } |
| 28 | let mask = CGEventMask(1 << CGEventType.flagsChanged.rawValue) | CGEventMask(1 << CGEventType.keyDown.rawValue) |
| 29 | let callback: CGEventTapCallBack = { _, type, event, info in |
| 30 | guard let info else { return Unmanaged.passUnretained(event) } |
| 31 | let me = Unmanaged<DictationKeys>.fromOpaque(info).takeUnretainedValue() |
| 32 | let swallow = MainActor.assumeIsolated { me.handle(type, event) } // the tap runs on the main run loop |
| 33 | return swallow ? nil : Unmanaged.passUnretained(event) |
| 34 | } |
| 35 | guard let t = CGEvent.tapCreate(tap: .cgSessionEventTap, place: .headInsertEventTap, options: .defaultTap, |
| 36 | eventsOfInterest: mask, callback: callback, |
| 37 | userInfo: Unmanaged.passUnretained(self).toOpaque()) else { return } |
| 38 | tap = t |
| 39 | source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, t, 0) |
| 40 | CFRunLoopAddSource(CFRunLoopGetMain(), source, .commonModes) |
| 41 | CGEvent.tapEnable(tap: t, enable: true) |
| 42 | AppDelegate.log("dictation keys: watching Fn") |
| 43 | } |
| 44 | |
| 45 | private func stop() { |
| 46 | guard let t = tap else { return } |
| 47 | CGEvent.tapEnable(tap: t, enable: false) |
| 48 | if let source { CFRunLoopRemoveSource(CFRunLoopGetMain(), source, .commonModes) } |
| 49 | tap = nil; source = nil; fnHeld = false |
| 50 | AppDelegate.log("dictation keys: off") |
| 51 | } |
| 52 | |
| 53 | private func handle(_ type: CGEventType, _ e: CGEvent) -> Bool { |
| 54 | if type == .tapDisabledByTimeout || type == .tapDisabledByUserInput { |
| 55 | if let tap { CGEvent.tapEnable(tap: tap, enable: true) } |
| 56 | return false |
| 57 | } |
| 58 | let code = e.getIntegerValueField(.keyboardEventKeycode), now = CFAbsoluteTimeGetCurrent() |
| 59 | if type == .flagsChanged { |
| 60 | let fn = e.flags.contains(.maskSecondaryFn) |
| 61 | if code == Int64(kVK_Function) { |
| 62 | if fn && !fnHeld { fnDown(now, control: e.flags.contains(.maskControl)) } |
| 63 | else if !fn && fnHeld { fnUp(now) } |
| 64 | } else if (code == Int64(kVK_Control) || code == Int64(kVK_RightControl)), fnHeld, e.flags.contains(.maskControl) { |
| 65 | d.makeCommand() |
| 66 | } |
| 67 | return false |
| 68 | } |
| 69 | guard d.listening else { return false } |
| 70 | if code == Int64(kVK_Escape) { d.cancel(); pendingTapAt = 0; return true } |
| 71 | if fnHeld && code == Int64(kVK_Space) { d.lock(); ignoreRelease = true; return true } |
| 72 | if fnHeld && d.mode != .handsFree { usedAsModifier = true } // Fn+arrow, Fn+Deleteā¦: Fn is being a modifier |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | private func fnDown(_ now: Double, control: Bool) { |
| 77 | fnHeld = true; fnDownAt = now; usedAsModifier = false; ignoreRelease = false |
| 78 | if d.listening { |
| 79 | if d.mode == .handsFree { d.end(); ignoreRelease = true } // Fn again finishes hands-free |
| 80 | else if pendingTapAt > 0, now - pendingTapAt < 0.55 { d.lock(); ignoreRelease = true } // second tap: hands-free |
| 81 | pendingTapAt = 0 |
| 82 | return |
| 83 | } |
| 84 | d.begin(control ? .command : .hold) |
| 85 | } |
| 86 | |
| 87 | private func fnUp(_ now: Double) { |
| 88 | fnHeld = false |
| 89 | guard !ignoreRelease, d.listening, d.mode != .handsFree else { return } |
| 90 | if usedAsModifier { d.cancel(); return } |
| 91 | if now - fnDownAt < 0.28 { |
| 92 | // A quick tap. Keep listening for a moment: a second tap means hands-free, otherwise it was nothing. |
| 93 | pendingTapAt = now |
| 94 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.56) { [weak self] in |
| 95 | guard let self, self.pendingTapAt == now else { return } |
| 96 | self.pendingTapAt = 0 |
| 97 | if self.d.listening, self.d.mode == .hold, !self.fnHeld { self.d.cancel() } |
| 98 | } |
| 99 | return |
| 100 | } |
| 101 | d.end() |
| 102 | } |
| 103 | } |