diff --git a/README.md b/README.md index 8fe7be7..3360378 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,13 @@ python whisper-dictation.py -m large -k cmd_r+shift -l en The models are multilingual, and you can specify a two-letter language code (e.g., "no" for Norwegian) with the `-l` or `--language` option. Specifying the language can improve recognition accuracy, especially for smaller model sizes. +#### Replace macOS default dictation trigger key +You can use this app to replace macOS built-in dictation. Trigger to begin recording with a double click of Right Command key and stop recording with a single click of Right Command key. +```bash +python whisper-dictation.py -m large --k_double_cmd -l en +``` +To use this trigger, go to System Settings -> Keyboard, disable Dictation. If you double click Right Command key on any text field, macOS will ask whether you want to enable Dictation, so select Don't Ask Again. + ## Setting the App as a Startup Item To have the app run automatically when your computer starts, follow these steps: diff --git a/whisper-dictation.py b/whisper-dictation.py index 761da6e..93675c1 100644 --- a/whisper-dictation.py +++ b/whisper-dictation.py @@ -92,6 +92,25 @@ def on_key_release(self, key): elif key == self.key2: self.key2_pressed = False +class DoubleCommandKeyListener: + def __init__(self, app): + self.app = app + self.key = keyboard.Key.cmd_r + self.pressed = 0 + self.last_press_time = 0 + + def on_key_press(self, key): + is_listening = self.app.started + if key == self.key: + current_time = time.time() + if not is_listening and current_time - self.last_press_time < 0.5: # Double click to start listening + self.app.toggle() + elif is_listening: # Single click to stop listening + self.app.toggle() + self.last_press_time = current_time + + def on_key_release(self, key): + pass class StatusBarApp(rumps.App): def __init__(self, recorder, languages=None, max_time=None): @@ -186,6 +205,9 @@ def parse_args(): parser.add_argument('-k', '--key_combination', type=str, default='cmd_l+alt' if platform.system() == 'Darwin' else 'ctrl+alt', help='Specify the key combination to toggle the app. Example: cmd_l+alt for macOS ' 'ctrl+alt for other platforms. Default: cmd_r+alt (macOS) or ctrl+alt (others).') + parser.add_argument('--k_double_cmd', action='store_true', + help='If set, use double Right Command key press on macOS to toggle the app (double click to begin recording, single click to stop recording). ' + 'Ignores the --key_combination argument.') parser.add_argument('-l', '--language', type=str, default=None, help='Specify the two-letter language code (e.g., "en" for English) to improve recognition accuracy. ' 'This can be especially helpful for smaller model sizes. To see the full list of supported languages, ' @@ -217,7 +239,10 @@ def parse_args(): recorder = Recorder(transcriber) app = StatusBarApp(recorder, args.language, args.max_time) - key_listener = GlobalKeyListener(app, args.key_combination) + if args.k_double_cmd: + key_listener = DoubleCommandKeyListener(app) + else: + key_listener = GlobalKeyListener(app, args.key_combination) listener = keyboard.Listener(on_press=key_listener.on_key_press, on_release=key_listener.on_key_release) listener.start()