voice-dictation/src/voice_dictation/ui/hotkey_acceptance.py

57 lines
1.9 KiB
Python

"""Manual native input acceptance, without microphone, API, or profile changes."""
from __future__ import annotations
import json
from pathlib import Path
def run_input_check(output: Path) -> None:
"""Observe two real hotkeys and paste a fixed marker after the second one."""
from AppKit import NSApplication
from ApplicationServices import AXIsProcessTrusted
from PyObjCTools import AppHelper
from voice_dictation.platform.macos.autopaste import MacAutoPaste
from voice_dictation.platform.macos.hotkey_listener import MacHotkeyListener
from voice_dictation.ui.setup_acceptance import synthetic_clipboard
trusted = bool(AXIsProcessTrusted())
presses = 0
def report(stage: str) -> None:
output.write_text(json.dumps({"stage": stage, "trusted": trusted, "presses": presses}))
if not trusted:
report("permission_required")
return
NSApplication.sharedApplication()
listener = MacHotkeyListener()
def paste() -> None:
MacAutoPaste().paste()
report("pasted")
def toggle() -> None:
nonlocal presses
presses += 1
report("hotkey")
if presses == 2:
AppHelper.callLater(0.3, paste)
listener.register("cmd+space", toggle)
with synthetic_clipboard("[DICTATION]"):
listener.start()
AppHelper.callLater(0.5, report, "ready")
if output.name.startswith("auto-"):
# Invoke the exact registered callbacks; native paste still goes to the editor.
registered_callback = listener._hotkeys[0][-1]
AppHelper.callLater(2.0, registered_callback)
AppHelper.callLater(3.0, registered_callback)
AppHelper.callLater(5.0, AppHelper.stopEventLoop)
else:
AppHelper.callLater(45, AppHelper.stopEventLoop)
try:
AppHelper.runEventLoop()
finally:
listener.stop()