Skip to content

Commit

Permalink
Merge pull request #8 from Gintasz/main
Browse files Browse the repository at this point in the history
Add double click Right Command keyboard trigger
  • Loading branch information
foges authored Jun 8, 2024
2 parents 0f7b912 + 9eb4c62 commit 16b76b2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
27 changes: 26 additions & 1 deletion whisper-dictation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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, '
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 16b76b2

Please sign in to comment.