Global Hotkeys
Relevant source files
The following files were used as context for generating this wiki page:The Global Hotkeys system provides a flexible and powerful way for users to control VoiceInk's core recording functionality from anywhere in macOS. It allows for configurable keyboard shortcuts to start and stop transcription, utilizing both predefined modifier keys and fully custom key combinations. The system is designed to support two primary interaction modes: a "push-to-talk" mode via holding a key and a "hands-free" toggle mode via a quick tap.
The central component of this system is the HotkeyManager class, which orchestrates event monitoring, state management, and interaction with the WhisperState to control the recording lifecycle. It leverages the KeyboardShortcuts library for custom shortcuts and NSEvent monitoring for modifier keys and mouse events.
Architecture
The hotkey system is primarily managed by HotkeyManager, which is initialized at the application's launch. It interacts with WhisperState to trigger recording actions and uses UserDefaults for persistence.
Sources: VoiceInk/HotkeyManager.swift, VoiceInk/VoiceInk.swift:74-77
Core Components
| Component | Description | File |
|---|---|---|
HotkeyManager | The main class that manages all hotkey logic, including event monitoring, state tracking, and delegating actions to WhisperState. | VoiceInk/HotkeyManager.swift |
WhisperState | The central state manager for the application's recording and transcription lifecycle. HotkeyManager calls its handleToggleMiniRecorder() method. | VoiceInk/HotkeyManager.swift:42 |
KeyboardShortcuts | An external library used to define and manage user-configurable custom keyboard shortcuts. | VoiceInk/HotkeyManager.swift:2 |
SettingsView | The SwiftUI view where users can configure their preferred hotkeys, including modifier keys and custom shortcuts. | VoiceInk/Views/Settings/SettingsView.swift |
ImportExportService | Handles the persistence of hotkey settings during backup and restore operations. | VoiceInk/Services/ImportExportService.swift |
Class Diagram
This diagram shows the relationship between the main components of the hotkey system.
Hotkey Configuration
Users can configure up to two primary hotkeys and several secondary shortcuts through the "Settings" panel. The primary hotkeys support both predefined modifier keys and custom combinations.
Sources: VoiceInk/Views/Settings/SettingsView.swift:22-72
Primary Hotkeys
The application allows for two primary hotkeys (Hotkey 1 and Hotkey 2) to trigger the main recording function. Each can be set to one of the predefined modifier keys or a custom shortcut.
The available modifier key options are defined in the HotkeyManager.HotkeyOption enum.
// Source: VoiceInk/HotkeyManager.swift:80-117
enum HotkeyOption: String, CaseIterable {
case none = "none"
case rightOption = "rightOption"
case leftOption = "leftOption"
case leftControl = "leftControl"
case rightControl = "rightControl"
case fn = "fn"
case rightCommand = "rightCommand"
case rightShift = "rightShift"
case custom = "custom"
var displayName: String { ... }
var keyCode: CGKeyCode? { ... }
var isModifierKey: Bool { ... }
}
| Option | Display Name | Key Code |
|---|---|---|
none | "None" | nil |
rightOption | "Right Option (⌥)" | 0x3D |
leftOption | "Left Option (⌥)" | 0x3A |
leftControl | "Left Control (⌃)" | 0x3B |
rightControl | "Right Control (⌃)" | 0x3E |
fn | "Fn" | 0x3F |
rightCommand | "Right Command (⌘)" | 0x36 |
rightShift | "Right Shift (⇧)" | 0x3C |
custom | "Custom" | nil |
Sources: VoiceInk/HotkeyManager.swift:80-117
When a user selects "Custom", the KeyboardShortcuts.Recorder view is displayed, allowing them to record a unique key combination. These are registered as toggleMiniRecorder and toggleMiniRecorder2.
Sources: VoiceInk/Views/Settings/SettingsView.swift:392-394, VoiceInk/HotkeyManager.swift:5-6
Middle-Click Toggle
In addition to keyboard hotkeys, users can enable a middle-mouse-click to toggle recording. This feature includes a configurable activation delay to prevent accidental triggers.
Sources: VoiceInk/Views/Settings/SettingsView.swift:160-192, VoiceInk/HotkeyManager.swift:186-215
Other Shortcuts
The application defines several other global shortcuts for convenience. These are configured in the "Other App Shortcuts" section of the settings.
| Shortcut Name | Default | Description |
|---|---|---|
pasteLastTranscription | Not set | Pastes the most recent original transcription. |
pasteLastEnhancement | Not set | Pastes the AI-enhanced transcript if available, otherwise the original. |
retryLastTranscription | Not set | Re-transcribes the last recorded audio. |
cancelRecorder | Double-tap Esc (default) or custom | Cancels the current recording session. |
Sources: VoiceInk/HotkeyManager.swift:7-11, VoiceInk/Views/Settings/SettingsView.swift:82-158
Event Handling and Logic
HotkeyManager uses different strategies to monitor for hotkey events depending on the configuration.
Sources: VoiceInk/HotkeyManager.swift:145-160
Event Monitoring Flow
The setupHotkeyMonitoring function is the entry point for establishing event listeners. It removes any existing monitors and then sets up new ones based on the current user settings.
Tap vs. Hold Logic (Push-to-Talk)
A key feature of the primary hotkeys is the dual-mode operation, which distinguishes between a quick tap and a longer press-and-hold action.
- Key Down: The
keyPressStartTimeis recorded. If not already in a recording session, a new one is started. - Key Up: The duration of the press is calculated (
pressDuration).- If
pressDurationis less thanbriefPressThreshold(1.7 seconds), the system enters "hands-free mode". The recording continues until the hotkey is tapped again. - If
pressDurationis greater than or equal to the threshold, it's treated as "push-to-talk". The recording stops immediately upon key release.
- If
This logic is implemented in processKeyPress for modifier keys and handleCustomShortcutKeyDown/Up for custom shortcuts.
Sources: VoiceInk/HotkeyManager.swift:60-61, 280-316, 318-360
The following diagram illustrates the state transitions for this logic.
Sources: VoiceInk/HotkeyManager.swift:280-316
Sequence Diagram: Modifier Key Press
This diagram shows the sequence of events when a user presses and releases a configured modifier key.
Sources: VoiceInk/HotkeyManager.swift:260-316
Data Persistence
Hotkey settings are persisted across application launches using UserDefaults and are included in the application's import/export feature.
UserDefaults
The HotkeyManager saves the raw string value of the selected HotkeyOption enum and the state of the middle-click toggle directly to UserDefaults within the property's didSet observer.
// Source: VoiceInk/HotkeyManager.swift:15-19
@Published var selectedHotkey1: HotkeyOption {
didSet {
UserDefaults.standard.set(selectedHotkey1.rawValue, forKey: "selectedHotkey1")
setupHotkeyMonitoring()
}
}
Sources: VoiceInk/HotkeyManager.swift:15-36
Import & Export
The ImportExportService gathers all hotkey-related settings into a GeneralSettings struct, which is then encoded into a JSON file for export. This includes the KeyboardShortcuts.Shortcut objects for custom hotkeys and the raw values of the selected modifier keys.
GeneralSettings Property | Description | Source |
|---|---|---|
toggleMiniRecorderShortcut | The custom shortcut for Hotkey 1. | ImportExportService.swift:9-10 |
toggleMiniRecorderShortcut2 | The custom shortcut for Hotkey 2. | ImportExportService.swift:10 |
retryLastTranscriptionShortcut | The custom shortcut for the retry action. | ImportExportService.swift:11 |
selectedHotkey1RawValue | The raw string value of the HotkeyOption for Hotkey 1. | ImportExportService.swift:12 |
selectedHotkey2RawValue | The raw string value of the HotkeyOption for Hotkey 2. | ImportExportService.swift:13 |
During import, the service decodes this data and applies it back to KeyboardShortcuts and the HotkeyManager instance.
Sources: VoiceInk/Services/ImportExportService.swift:8-28, 93-100, 169-184