hex/src/suppression/hotkey.rs

1335 lines
51 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! The dictation shortcut state machine: hold, double-tap lock, Toggle,
//! chord discards, stale-key recovery, and the chord breaker.
use super::*;
use crate::caps_lock_dictation::{CapsAction, CapsState, press_action};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HotkeyAction {
Start,
Finish,
Discard(DiscardCause),
/// Hex ended the recording: its input was lost or suspended.
Cancel,
/// A cancel shortcut discarded the recording: Toggle's, an extra row's,
/// or Escape while a Hold capture is held.
CancelShortcut,
PasteLast,
}
impl HotkeyAction {
pub fn discard_cause(self) -> Option<DiscardCause> {
match self {
Self::Discard(cause) => Some(cause),
_ => None,
}
}
}
#[derive(Debug)]
enum State {
Idle {
// The last Recording release, kept only while double-tap lock may
// still pair the next press with it.
last_release_at: Option<CaptureInstant>,
},
FirstTapPressed {
started_at: CaptureInstant,
},
AwaitingSecondTap {
released_at: CaptureInstant,
},
SecondTapPressed {
first_released_at: CaptureInstant,
},
Recording {
started_at: CaptureInstant,
previous_release: Option<CaptureInstant>,
},
Locked,
Dirty,
}
impl State {
const IDLE: Self = Self::Idle {
last_release_at: None,
};
/// A double-tap-only gesture that has not yet locked.
fn is_pending_gesture(&self) -> bool {
matches!(
self,
Self::FirstTapPressed { .. }
| Self::AwaitingSecondTap { .. }
| Self::SecondTapPressed { .. }
)
}
}
/// Modifier changes seen since the keyboard last reported no modifiers.
#[derive(Clone, Copy)]
struct ModifierHistory {
flags: u64,
flags_at: CaptureInstant,
since_neutral: u64,
neutral_at: Option<CaptureInstant>,
}
impl ModifierHistory {
/// No evidence, and none from events stamped before `at`.
const fn reset(at: CaptureInstant) -> Self {
Self {
flags: 0,
flags_at: at,
since_neutral: 0,
neutral_at: None,
}
}
}
/// Live keyboard state, read only by the stale-press check.
#[derive(Clone, Copy)]
struct PhysicalKeyboard {
flags: fn() -> u64,
key_down: fn(u16) -> bool,
}
impl PhysicalKeyboard {
// Same source as `recover_stale_keys`. Unit tests never read the real
// keyboard (see `trigger_is_physically_down`); their default reads as
// everything up, so every test can take the fast path.
const SESSION: Self = if cfg!(test) {
Self {
flags: || 0,
key_down: |_| false,
}
} else {
Self {
// SAFETY: Read-only queries of a documented event-source state.
flags: || unsafe { CGEventSourceFlagsState(COMBINED_SESSION_STATE) },
key_down: input_is_held,
}
};
}
/// Labs › Tap or hold. Unit tests never read the saved Labs switches; they
/// turn it on per machine.
const TAP_OR_HOLD: fn() -> bool = if cfg!(test) {
|| false
} else {
crate::labs::tap_or_hold
};
/// Labs › Double-tap Option, Command, or Globe. Unit tests turn it on per machine,
/// as they do Tap or hold.
const DOUBLE_TAP_MODIFIERS: fn() -> bool = if cfg!(test) {
|| false
} else {
crate::labs::double_tap_modifiers
};
/// Labs › Hold through other keys. Unit tests turn it on per machine, as they
/// do Tap or hold.
const HOLD_THROUGH_KEYS: fn() -> bool = if cfg!(test) {
|| false
} else {
crate::labs::hold_through_keys
};
/// Keys and modifier sides currently tracked as held down, with when each went
/// down. Kept together so every clear resets keys and modifier sides alike.
#[derive(Default)]
struct HeldKeys {
keys: HashMap<u16, CaptureInstant>,
sides: HashMap<u64, CaptureInstant>,
}
impl HeldKeys {
fn clear(&mut self) {
self.keys.clear();
self.sides.clear();
}
fn clear_sides(&mut self) {
self.sides.clear();
}
fn update_sides(&mut self, flags: u64, at: CaptureInstant) {
for mask in MODIFIER_SIDE_MASKS {
if flags & mask == 0 {
self.sides.remove(&mask);
} else {
self.sides.entry(mask).or_insert(at);
}
}
}
fn recover(&mut self, sampled_flags: u64) {
self.keys.clear();
self.sides.retain(|&mask, _| sampled_flags & mask != 0);
}
/// Records a key press at `at`; returns `true` when the key was not already
/// tracked as down.
fn press(&mut self, code: u16, at: CaptureInstant) -> bool {
let fresh = !self.keys.contains_key(&code);
self.keys
.entry(code)
.and_modify(|down_at| *down_at = (*down_at).max(at))
.or_insert(at);
fresh
}
fn release(&mut self, code: u16) {
self.keys.remove(&code);
}
fn retain_keys(&mut self, mut keep: impl FnMut(&u16) -> bool) {
self.keys.retain(|code, _| keep(code));
}
fn contains_key(&self, code: u16) -> bool {
self.keys.contains_key(&code)
}
fn keys_empty(&self) -> bool {
self.keys.is_empty()
}
fn key_count(&self) -> usize {
self.keys.len()
}
fn key_codes(&self) -> impl Iterator<Item = &u16> {
self.keys.keys()
}
fn key_down_at(&self, code: u16) -> Option<CaptureInstant> {
self.keys.get(&code).copied()
}
fn side_down_at(&self, mask: u64) -> Option<CaptureInstant> {
self.sides.get(&mask).copied()
}
}
pub struct DictationHotkey {
state: State,
held: HeldKeys,
double_tap_enabled: bool,
double_tap_only: bool,
/// Press to start, press again to finish: releasing locks the capture.
toggle: bool,
/// Cancel works in every trigger mode, not only under Toggle: an extra
/// dictation shortcut has its own mode and always takes its cancel key.
cancel_in_any_mode: bool,
/// Labs › Tap or hold, read at each Toggle release: a press held past
/// the hold threshold finishes on release instead of locking.
tap_or_hold: fn() -> bool,
/// Labs › Double-tap Option, Command, or Globe, read as the mode is applied: a
/// modifier-only binding may start a double-tap-only capture.
double_tap_modifiers: fn() -> bool,
/// Labs › Hold through other keys, read as a key joins a held
/// modifier-only capture: a second or more in, the key is the app's, not a
/// chord.
hold_through_keys: fn() -> bool,
binding: RuntimeHotkey,
/// Toggle's cancel shortcut; `None` in Hold mode.
cancel: Option<RuntimeHotkey>,
ignore_before: Option<CaptureInstant>,
stale_keys_neutral_since: Option<CaptureInstant>,
recovery_ignore_through: Option<CaptureInstant>,
recovery_updated_keys: HashSet<u16>,
// Fn evidence comes only from modifier events; None marks a blind period.
// The timestamp prevents delayed events from restoring invalidated evidence.
function_modifier: (CaptureInstant, Option<bool>),
// Recent HID key presses, which another app's shortcut may have hidden.
physical_key_downs: VecDeque<(CaptureInstant, u16, HidTiming)>,
// When the held modifier-only press Hex owns is due its chord breaker.
chord_break_at: Option<CaptureInstant>,
modifier_history: ModifierHistory,
physical: PhysicalKeyboard,
}
impl DictationHotkey {
pub fn new(now: CaptureInstant, double_tap_enabled: bool, binding: RuntimeHotkey) -> Self {
Self::with_binding(
trigger_is_physically_down(binding),
now,
double_tap_enabled,
binding,
)
}
pub(crate) fn with_binding(
trigger_down: bool,
now: CaptureInstant,
double_tap_enabled: bool,
binding: RuntimeHotkey,
) -> Self {
Self {
state: if trigger_down {
State::Recording {
started_at: now,
previous_release: None,
}
} else {
State::IDLE
},
held: HeldKeys::default(),
double_tap_enabled,
double_tap_only: false,
toggle: false,
cancel_in_any_mode: false,
tap_or_hold: TAP_OR_HOLD,
double_tap_modifiers: DOUBLE_TAP_MODIFIERS,
hold_through_keys: HOLD_THROUGH_KEYS,
binding,
cancel: None,
ignore_before: None,
stale_keys_neutral_since: None,
recovery_ignore_through: None,
recovery_updated_keys: HashSet::new(),
function_modifier: (CaptureInstant::ZERO, None),
physical_key_downs: VecDeque::new(),
chord_break_at: None,
modifier_history: ModifierHistory::reset(CaptureInstant::ZERO),
physical: PhysicalKeyboard::SESSION,
}
}
pub fn is_recording(&self) -> bool {
matches!(self.state, State::Recording { .. } | State::Locked)
}
/// The cancel shortcut this machine acts on now: only while it records,
/// in Toggle mode or as an extra row.
pub fn recording_cancel(&self) -> Option<RuntimeHotkey> {
(self.is_recording() && (self.toggle || self.cancel_in_any_mode))
.then_some(self.cancel)
.flatten()
}
/// Hold mode's cancel: Escape while the shortcut is still held, once the
/// hold threshold made the press Hex's. Earlier, Escape may belong to a
/// chord such as Option-Escape; a hands-free capture never takes it, as
/// Escape there may be meant for the app in front (#786).
fn escape_cancels_hold(&self, now: CaptureInstant) -> bool {
!self.toggle
&& matches!(self.state, State::Recording { started_at, .. }
if now.duration_since(started_at) >= MINIMUM_HOLD_DURATION)
}
/// Whether the tap keeps Escape from the app in front at `now`: it
/// cancels this machine's held capture.
pub fn takes_escape(&self, now: CaptureInstant) -> bool {
self.escape_cancels_hold(now)
}
/// An Escape a recording machine saw and did nothing with, and why.
fn log_ignored_escape(&self, now: CaptureInstant) {
let reason = match self.state {
State::Recording { .. } if self.toggle => "Toggle's cancel shortcut is another key",
State::Recording { .. } => "before the hold threshold",
State::Locked if self.toggle || self.cancel_in_any_mode => {
"the cancel shortcut is another key"
}
State::Locked => "a hands-free capture in Hold mode has no cancel",
_ => return,
};
let after_press_ms = match self.state {
State::Recording { started_at, .. } => Some(signed_ms(now, started_at)),
_ => None,
};
tracing::info!(
shortcut = "dictation",
reason,
after_press_ms,
"Escape ignored while recording"
);
}
/// Whether a double-tap gesture is in flight and still waiting for its
/// second tap.
pub fn has_pending_gesture(&self) -> bool {
self.state.is_pending_gesture()
}
/// Recording hands-free: locked by a double tap, a Toggle release, or
/// `hex dictation start`.
pub fn is_locked(&self) -> bool {
matches!(self.state, State::Locked)
}
/// Labs › Stop phrase: ends a hands-free capture as its finishing press
/// does. No key is down when the words finish it, so the machine goes
/// idle as the Caps Lock finish and a suspend leave it, rather than
/// waiting on a stale-key recovery. A held press records on, and an idle
/// machine does nothing.
pub fn finish_hands_free(&mut self) -> Option<HotkeyAction> {
if !self.is_locked() {
return None;
}
self.state = State::IDLE;
self.stale_keys_neutral_since = None;
self.chord_break_at = None;
Some(HotkeyAction::Finish)
}
pub fn suppresses_recognition(&self) -> bool {
!matches!(self.state, State::Idle { .. })
}
pub fn set_double_tap_enabled(&mut self, enabled: bool) {
self.double_tap_enabled = enabled;
if !enabled {
match &mut self.state {
State::Idle { last_release_at } => *last_release_at = None,
State::Recording {
previous_release, ..
} => *previous_release = None,
_ => {}
}
}
}
pub fn set_double_tap_only(&mut self, enabled: bool) {
let allowed = self.binding.key_code.is_some() || (self.double_tap_modifiers)();
self.double_tap_only = enabled && allowed && self.double_tap_enabled;
// Active captures still need their release to reach the audio owner.
if !self.double_tap_only && self.state.is_pending_gesture() {
self.state = State::IDLE;
}
}
/// Takes effect from the next press; an active capture keeps its gesture.
pub fn set_toggle(&mut self, enabled: bool) {
self.toggle = enabled;
}
/// Applies a Labs › More dictation shortcuts row's activation mode.
/// Double-tap is enabled first because double-tap only reads it, then
/// double-tap only, then Toggle.
pub fn set_trigger(&mut self, mode: crate::app_settings::ExtraDictationMode) {
let (toggle, double_tap_enabled, double_tap_only) = mode.switches();
self.set_double_tap_enabled(double_tap_enabled);
self.set_double_tap_only(double_tap_only);
self.set_toggle(toggle);
}
/// Whether this machine takes its cancel shortcut in every trigger mode,
/// as an extra dictation shortcut does.
pub fn set_cancel_in_any_mode(&mut self, enabled: bool) {
self.cancel_in_any_mode = enabled;
}
/// Toggle's or an extra shortcut's cancel shortcut, or `None` for no
/// cancel.
pub fn set_cancel(&mut self, cancel: Option<RuntimeHotkey>) {
self.cancel = cancel;
}
/// The shortcut this machine matches, for rebinding a machine by its
/// binding.
pub fn binding(&self) -> RuntimeHotkey {
self.binding
}
pub fn set_binding(&mut self, binding: RuntimeHotkey) {
if matches!(self.state, State::Idle { .. }) && self.binding != binding {
self.binding = binding;
self.state = State::IDLE;
}
}
pub fn suspend(&mut self) -> Option<HotkeyAction> {
let was_recording = self.is_recording();
let now = CaptureInstant::now();
self.function_modifier = (now, None);
self.modifier_history = ModifierHistory::reset(now);
self.state = State::IDLE;
self.held.clear();
self.stale_keys_neutral_since = None;
self.chord_break_at = None;
was_recording.then_some(HotkeyAction::Cancel)
}
/// Locks an idle machine without a press, as a `hex dictation start`
/// does: the next shortcut press finishes it, the same as a double-tap
/// lock. Returns false while a press is in flight.
pub fn lock(&mut self) -> bool {
if !matches!(self.state, State::Idle { .. }) && !self.state.is_pending_gesture() {
return false;
}
self.state = State::Locked;
self.held.clear();
self.stale_keys_neutral_since = None;
self.chord_break_at = None;
true
}
/// A physical Caps Lock press (Labs › Caps Lock dictation). From Idle it
/// locks a hands-free capture; from a lock it finishes it. A hold
/// capture, a pending double tap, or another recording is left alone.
pub fn caps_lock_toggle(&mut self) -> Option<HotkeyAction> {
let state = match &self.state {
State::Idle { .. } => CapsState::Idle,
State::Locked => CapsState::Recording,
_ => CapsState::Busy,
};
match press_action(state) {
CapsAction::Start => self.lock().then_some(HotkeyAction::Start),
CapsAction::Finish => {
self.state = State::IDLE;
self.held.clear();
self.stale_keys_neutral_since = None;
self.chord_break_at = None;
Some(HotkeyAction::Finish)
}
CapsAction::Ignore => None,
}
}
pub fn disarm_pending_gesture(&mut self) {
if !self.is_recording() {
if matches!(self.state, State::Idle { .. }) || self.state.is_pending_gesture() {
self.state = State::IDLE;
}
self.stale_keys_neutral_since = None;
}
}
#[cfg(test)]
pub fn suppress_until_release(&mut self) {
self.state = State::Dirty;
self.stale_keys_neutral_since = None;
self.chord_break_at = None;
}
// Call only after draining input. Polling repairs bookkeeping, never capture boundaries.
pub fn recover_stale_keys(&mut self) {
self.recover_stale_keys_with(
CaptureInstant::now,
// Match the annotated-session tap, including assistive keyboard input.
|| unsafe { CGEventSourceFlagsState(COMBINED_SESSION_STATE) },
input_is_held,
);
}
fn recover_stale_keys_with(
&mut self,
now: impl FnOnce() -> CaptureInstant,
mut flags: impl FnMut() -> u64,
mut key_down: impl FnMut(u16) -> bool,
) {
// Arrow key metadata can leave SecondaryFn set after release. Only ignore
// it when modifier-change events independently say Fn is not held.
let modifiers_mask = if self.function_modifier.1 == Some(false) {
HOTKEY_MODIFIERS_MASK & !crate::app_settings::FUNCTION_KEY_MASK
} else {
HOTKEY_MODIFIERS_MASK
};
// A missed modifier release would make the next real press of it
// read as no change.
let stale_modifiers = self.modifier_history.flags & modifiers_mask != 0;
let recoverable = match self.state {
State::Dirty => true,
// A lock captures while the keyboard sits idle. A missed key-up would
// otherwise block the fresh shortcut press that finishes it.
State::Idle { .. } | State::Locked => !self.held.keys_empty() || stale_modifiers,
_ => false,
};
if !recoverable {
self.stale_keys_neutral_since = None;
return;
}
// A missing modifier release can leave Dirty with no ordinary keys tracked.
// Avoid a full scan when a tracked key is still physically held.
let sampled_flags = flags();
if sampled_flags & modifiers_mask != 0
|| (!self.held.keys_empty() && self.held.key_codes().all(|&code| key_down(code)))
|| (0..128).any(&mut key_down)
// Mouse triggers share tracked keys; a stale keyboard entry must
// not cause recovery while a tracked mouse button is still down.
|| self.held.key_codes().any(|&code| {
crate::app_settings::HotkeyKey::mouse_button(code).is_some() && key_down(code)
})
|| flags() & modifiers_mask != 0
{
self.stale_keys_neutral_since = None;
return;
}
let sampled_through = now();
let Some(neutral_since) = self.stale_keys_neutral_since else {
self.stale_keys_neutral_since = Some(sampled_through);
return;
};
if sampled_through.duration_since(neutral_since) < STALE_KEY_NEUTRAL_DURATION {
return;
}
let locked = matches!(self.state, State::Locked);
let key_codes: Vec<u16> = self.held.key_codes().copied().collect();
// A system shortcut such as Command-Tab swallows its key's release.
tracing::warn!(
key_count = self.held.key_count(),
key_codes = ?key_codes,
locked,
"resynchronized stale input tracking after neutral keyboard"
);
self.held.recover(sampled_flags);
// The scan is not atomic, so it rebuilds the held modifiers but is no
// neutral evidence for keys whose delayed edges may still arrive.
self.modifier_history.flags = sampled_flags;
self.modifier_history.flags_at = sampled_through;
// Repair bookkeeping only: a lock keeps capturing until a fresh press.
if !locked {
self.state = State::IDLE;
}
self.chord_break_at = None;
self.stale_keys_neutral_since = None;
self.recovery_ignore_through = Some(sampled_through);
self.recovery_updated_keys.clear();
}
#[cfg(test)]
fn wait_for_release_with_state(&mut self, now: CaptureInstant, flags: u64, key_down: bool) {
self.suspend();
self.ignore_before = Some(now);
if key_down && let Some(code) = self.binding.key_code {
self.held.press(code, now);
}
if flags & HOTKEY_MODIFIERS_MASK != 0 || !self.held.keys_empty() {
self.state = State::Dirty;
}
}
// Suspended shortcut matching must still observe releases of previously held keys.
pub fn track_key_state(&mut self, event: InputEvent, at: CaptureInstant) -> Option<bool> {
if let InputEvent::Key { code, .. } | InputEvent::PhysicalKeyDown { code, .. } = event
&& is_own_globe_key(code, self.binding)
{
return None;
}
self.stale_keys_neutral_since = None;
let fenced = self.ignore_before.is_some_and(|changed_at| at < changed_at)
|| self
.recovery_ignore_through
.is_some_and(|fence| at <= fence);
if let InputEvent::Flags(flags) = event {
self.held.update_sides(flags, at);
} else if matches!(event, InputEvent::TapDisabled) {
self.held.clear_sides();
}
if let InputEvent::Flags(flags) = event
&& at >= self.function_modifier.0
{
self.function_modifier = (
at,
Some(flags & crate::app_settings::FUNCTION_KEY_MASK != 0),
);
} else if matches!(event, InputEvent::TapDisabled) {
let now = CaptureInstant::now();
self.function_modifier = (now, None);
self.modifier_history = ModifierHistory::reset(now);
}
// Only modifier changes: key events can carry Fn as navigation metadata.
// Fenced edges predate the evidence that replaced them.
if let InputEvent::Flags(flags) = event
&& !fenced
&& at >= self.modifier_history.flags_at
{
let history = &mut self.modifier_history;
history.flags = flags;
history.flags_at = at;
if flags & HOTKEY_MODIFIERS_MASK == 0 {
history.since_neutral = 0;
history.neutral_at = Some(at);
} else {
history.since_neutral |= flags & HOTKEY_MODIFIERS_MASK;
}
}
if let InputEvent::Key { code, .. } = event
&& let Some(fence) = self.recovery_ignore_through
{
// A delayed pre-recovery release must not erase a newer press of that key.
if at <= fence && self.recovery_updated_keys.contains(&code) {
return None;
}
if at > fence {
self.recovery_updated_keys.insert(code);
}
}
Some(match event {
InputEvent::Key { code, down, .. } => {
if down {
self.held.press(code, at)
} else {
self.held.release(code);
false
}
}
InputEvent::Flags(_)
| InputEvent::PhysicalKeyDown { .. }
| InputEvent::MouseDown
| InputEvent::TapDisabled => false,
})
}
fn log_hid(
&self,
message: &'static str,
code: u16,
at: CaptureInstant,
timing: HidTiming,
press_at: CaptureInstant,
) {
tracing::info!(
shortcut = "dictation",
cause = DiscardCause::HidKey.as_str(),
key_code = code,
after_press_ms = signed_ms(at, press_at),
hid_raw = timing.raw,
hid_clock = timing.clock.as_str(),
hid_received_after_press_ms = signed_ms(timing.received_at, press_at),
hid_arrival_delay_ms = signed_ms(timing.received_at, at),
"{message}"
);
}
pub fn process(&mut self, event: InputEvent, now: CaptureInstant) -> Option<HotkeyAction> {
let was_locked = matches!(self.state, State::Locked);
let action = self.process_event(event, now);
self.track_chord_break(event, now, action, was_locked);
action
}
/// A bare modifier passes through to other apps, so a hold reads to them
/// as a tap on release (Raycast's Option-tap hotkey, for one). Once Hex
/// owns the press (a hold past the threshold, a Toggle start past the
/// chord grace, or the press that finishes a lock) it is owed one chord
/// breaker while still held.
fn track_chord_break(
&mut self,
event: InputEvent,
now: CaptureInstant,
action: Option<HotkeyAction>,
was_locked: bool,
) {
if self.binding.key_code.is_some() || self.binding.modifiers.function {
self.chord_break_at = None;
return;
}
let released = match event {
InputEvent::Flags(flags) | InputEvent::Key { flags, .. } => {
!self.binding.required_modifiers_down(flags)
}
InputEvent::TapDisabled => true,
InputEvent::PhysicalKeyDown { .. } | InputEvent::MouseDown => false,
};
match action {
Some(HotkeyAction::Start) if matches!(self.state, State::Recording { .. }) => {
// Toggle's press is usually a short tap, so it can't wait out
// the hold; the grace lets an extra modifier pressed with it
// discard it first.
self.chord_break_at = Some(if self.toggle {
now + crate::feedback::CHORD_GRACE
} else {
now + MINIMUM_HOLD_DURATION
});
}
Some(HotkeyAction::Finish) if was_locked && !released => {
self.chord_break_at = Some(now);
}
Some(_) => self.chord_break_at = None,
None if released => self.chord_break_at = None,
None => {}
}
}
/// Whether the held press is due its chord breaker at `now`; true at most
/// once per press.
pub fn take_chord_break(&mut self, now: CaptureInstant) -> bool {
let due = self.chord_break_at.is_some_and(|due_at| now >= due_at);
if due {
self.chord_break_at = None;
}
due
}
/// Posts the chord breaker the held press is due, if exactly its modifiers
/// are still physically down; any other modifier already breaks the tap.
/// The posted key carries Hex's marker, so neither shortcut machine sees
/// it, and the key tap drops it before the focused app.
pub fn break_chord(&mut self, now: CaptureInstant) {
if !self.take_chord_break(now) {
return;
}
// SAFETY: A read-only query of the documented HID system state.
let flags = unsafe { CGEventSourceFlagsState(HID_SYSTEM_STATE) };
if !self.binding.exact_modifiers(flags) {
return;
}
if let Err(error) = crate::keyboard::post_chord_breaker(flags) {
tracing::warn!(shortcut = "dictation", %error, "chord breaker failed");
}
}
fn process_event(&mut self, event: InputEvent, now: CaptureInstant) -> Option<HotkeyAction> {
self.stale_keys_neutral_since = None;
if matches!(event, InputEvent::TapDisabled) {
let was_recording = self.is_recording();
let now = CaptureInstant::now();
self.function_modifier = (now, None);
self.modifier_history = ModifierHistory::reset(now);
self.state = State::Dirty;
return was_recording.then_some(HotkeyAction::Cancel);
}
// Queued edges from before an opt-in transition cannot start a new capture.
if self
.ignore_before
.is_some_and(|changed_at| now < changed_at)
{
return None;
}
let prior = self.modifier_history;
let fresh_key_down = self.track_key_state(event, now)?;
let binding_press = self.binding_press(event, fresh_key_down, prior.flags);
if self
.recovery_ignore_through
.is_some_and(|sampled_through| now <= sampled_through)
{
if binding_press {
self.log_ignored_press("inside a stale-key recovery window");
}
// A key can go down during the non-atomic scan. Retain its edge but
// require a later neutral event before accepting another gesture.
// An old neutral release cannot invalidate the recovery's neutral sample.
let neutral = matches!(
event,
InputEvent::Flags(flags) | InputEvent::Key { flags, .. }
if flags & HOTKEY_MODIFIERS_MASK == 0 && self.held.keys_empty()
);
if !neutral && matches!(self.state, State::Idle { .. } | State::Dirty) {
self.state = State::Dirty;
}
return None;
}
if fresh_key_down
&& let Some(action) = paste_action(event, crate::app_settings::runtime_hotkeys())
{
self.state = State::Dirty;
return Some(action);
}
// Toggle's cancel shortcut discards the recording, and an extra
// shortcut takes its own in every trigger mode. The tap kept the key
// from the app in front.
if fresh_key_down
&& (self.toggle || self.cancel_in_any_mode)
&& self.is_recording()
&& let InputEvent::Key {
code,
down: true,
flags,
} = event
&& self
.cancel
.is_some_and(|cancel| cancel.matches_key_press(code, flags))
{
self.state = State::Dirty;
return Some(HotkeyAction::CancelShortcut);
}
if fresh_key_down && event.is_escape_down() {
if let State::Recording { started_at, .. } = self.state
&& self.escape_cancels_hold(now)
{
tracing::info!(
shortcut = "dictation",
after_press_ms = signed_ms(now, started_at),
"dictation cancelled by Escape while held"
);
self.state = State::Dirty;
return Some(HotkeyAction::CancelShortcut);
}
self.log_ignored_escape(now);
}
let ignored_because = binding_press
.then(|| self.resync_stale_press(now))
.flatten();
let trigger_pressed = self.trigger_pressed(event, fresh_key_down, prior.flags);
if let InputEvent::PhysicalKeyDown { code, timing } = event
&& self.binding.key_code != Some(code)
{
if self.physical_key_downs.len() == PHYSICAL_KEY_DOWN_HISTORY {
self.physical_key_downs.pop_front();
}
self.physical_key_downs.push_back((now, code, timing));
}
// HID evidence can overtake the session event of the press it follows.
if trigger_pressed
&& matches!(
self.state,
State::Idle { .. } | State::AwaitingSecondTap { .. }
)
&& let Some(&(at, code, timing)) = self.physical_key_downs.iter().find(|(at, ..)| {
at.checked_duration_since(now)
.is_some_and(|after| after > Duration::ZERO && after < MINIMUM_HOLD_DURATION)
})
{
self.log_hid(
"dictation press dropped: HID key evidence overtook it",
code,
at,
timing,
now,
);
self.state = State::Dirty;
return None;
}
let flags = match event {
InputEvent::Flags(flags) | InputEvent::Key { flags, .. } => Some(flags),
InputEvent::PhysicalKeyDown { .. }
| InputEvent::MouseDown
| InputEvent::TapDisabled => None,
};
let trigger_down = flags.is_some_and(|flags| {
self.binding.required_modifiers_down(flags)
&& self
.binding
.key_code
.is_none_or(|code| self.held.contains_key(code))
});
let trigger_released = flags.is_some() && !trigger_down;
let unrelated_key_down = matches!(
event,
InputEvent::Key {
code,
down: true,
..
} | InputEvent::PhysicalKeyDown { code, .. } if self.binding.key_code != Some(code)
);
let extra_modifiers = flags.is_some_and(|flags| {
self.binding.required_modifiers_down(flags) && !self.binding.exact_modifiers(flags)
});
// Escape is never a chord: it cancels only a held capture, above (#786).
let chord_key_down = unrelated_key_down
&& !matches!(
event,
InputEvent::Key {
code: ESCAPE_KEY_CODE,
..
} | InputEvent::PhysicalKeyDown {
code: ESCAPE_KEY_CODE,
..
}
);
let action = match self.state {
State::Idle { .. } if self.double_tap_only && !self.toggle && trigger_pressed => {
self.state = State::FirstTapPressed { started_at: now };
None
}
State::FirstTapPressed { started_at } if trigger_released => {
// A lone modifier held past the hold threshold is a hold,
// not a tap: it neither arms the double tap nor records.
if self.binding.key_code.is_none()
&& now.duration_since(started_at) >= MINIMUM_HOLD_DURATION
{
self.state = State::IDLE;
} else {
self.state = State::AwaitingSecondTap { released_at: now };
}
None
}
State::AwaitingSecondTap { released_at }
if trigger_pressed && now.duration_since(released_at) < DOUBLE_TAP_WINDOW =>
{
self.state = State::SecondTapPressed {
first_released_at: released_at,
};
None
}
State::SecondTapPressed { first_released_at }
if trigger_released
&& now.duration_since(first_released_at) < DOUBLE_TAP_WINDOW =>
{
self.state = State::Locked;
Some(HotkeyAction::Start)
}
State::SecondTapPressed { .. } if trigger_released => {
self.state = State::IDLE;
None
}
State::AwaitingSecondTap { .. }
| State::FirstTapPressed { .. }
| State::SecondTapPressed { .. }
if unrelated_key_down
|| extra_modifiers
|| matches!(event, InputEvent::MouseDown)
// A modifier or side the binding doesn't cover between a
// keyless chord's taps is a chord too, such as Option,
// Command, Option. The binding's own keys may go down and
// up one at a time (⌃ then ⌥, or both Options).
|| (self.binding.key_code.is_none()
&& flags.is_some_and(|flags| {
flags & HOTKEY_MODIFIERS_MASK != 0
&& !self.binding.covers_modifiers(flags)
})) =>
{
self.state = State::Dirty;
None
}
State::AwaitingSecondTap { released_at }
if now.duration_since(released_at) >= DOUBLE_TAP_WINDOW =>
{
self.state = if trigger_pressed {
State::FirstTapPressed { started_at: now }
} else {
State::IDLE
};
None
}
State::Locked if trigger_pressed => {
self.state = State::Dirty;
Some(HotkeyAction::Finish)
}
State::Idle { last_release_at } if trigger_pressed => {
let previous_release = last_release_at.filter(|released| {
self.double_tap_enabled && now.duration_since(*released) < DOUBLE_TAP_WINDOW
});
self.state = State::Recording {
started_at: now,
previous_release,
};
Some(HotkeyAction::Start)
}
// With Labs › Tap or hold, a Toggle press held past the hold
// threshold is push-to-talk: it falls through to finish below.
State::Recording { started_at, .. }
if trigger_released
&& self.toggle
&& (now.duration_since(started_at) < MINIMUM_HOLD_DURATION
|| !(self.tap_or_hold)()) =>
{
self.state = State::Locked;
None
}
State::Recording {
previous_release: Some(released),
..
} if trigger_released && now.duration_since(released) < DOUBLE_TAP_WINDOW => {
self.state = State::Locked;
None
}
State::Recording { .. } if trigger_released => {
self.state = State::Idle {
last_release_at: self.double_tap_enabled.then_some(now),
};
Some(HotkeyAction::Finish)
}
State::Recording { started_at, .. }
// Delayed HID evidence from before the press is not a chord.
if !(matches!(event, InputEvent::PhysicalKeyDown { .. }) && now < started_at)
&& ((now.duration_since(started_at) < MINIMUM_HOLD_DURATION
&& (chord_key_down
|| matches!(event, InputEvent::MouseDown)
|| extra_modifiers))
// A held modifier-only shortcut plus any key is a
// chord at any time (Spaceman's Option-1 hides its
// key from the session), except Return once the
// hold made the press Hex's: pressing it while
// dictating never throws the dictation away. With
// Labs › Hold through other keys, a key a
// second or more into the hold is the app's too, and
// the release still finishes.
|| (chord_key_down
&& !is_return_key(event)
&& self.binding.key_code.is_none()
&& !self.toggle
&& !crate::dictation::holds_through_joining_keys(
(self.hold_through_keys)(),
self.toggle,
now.duration_since(started_at),
))) =>
{
self.state = State::Dirty;
let cause = match event {
InputEvent::PhysicalKeyDown { code, timing } => {
self.log_hid(
"dictation press discarded as a chord",
code,
now,
timing,
started_at,
);
DiscardCause::HidKey
}
InputEvent::Key { code, .. } if chord_key_down => {
tracing::info!(
shortcut = "dictation",
cause = DiscardCause::Key.as_str(),
key_code = code,
after_press_ms = signed_ms(now, started_at),
"dictation press discarded as a chord"
);
DiscardCause::Key
}
_ => {
let cause = if matches!(event, InputEvent::MouseDown) {
DiscardCause::Mouse
} else {
DiscardCause::ExtraModifier
};
tracing::info!(
shortcut = "dictation",
cause = cause.as_str(),
modifier_flags =
format_args!("{:#x}", flags.unwrap_or(0) & HOTKEY_MODIFIERS_MASK),
after_press_ms = signed_ms(now, started_at),
"dictation press discarded as a chord"
);
cause
}
};
Some(HotkeyAction::Discard(cause))
}
State::Dirty
if flags.is_some_and(|flags| flags & HOTKEY_MODIFIERS_MASK == 0)
&& self.held.keys_empty() =>
{
self.state = State::IDLE;
None
}
_ => None,
};
if action.is_none()
&& let Some(reason) = ignored_because
{
self.log_ignored_press(reason);
}
action
}
/// A down edge of the binding, whether or not tracked keys block it.
fn binding_press(&self, event: InputEvent, fresh_key_down: bool, prior_flags: u64) -> bool {
match self.binding.key_code {
Some(_) => self.trigger_pressed(event, fresh_key_down, prior_flags),
None => self.modifiers_went_down(event, prior_flags),
}
}
/// The modifier-only binding's modifiers went down, from up, to exactly
/// themselves. Releasing Shift off a held Shift-Option is no Option press,
/// nor is Caps Lock or the other Option changing while Option is held.
fn modifiers_went_down(&self, event: InputEvent, prior_flags: u64) -> bool {
matches!(event, InputEvent::Flags(flags) if self.binding.exact_modifiers(flags))
&& !self.binding.modifiers.is_empty()
&& !self.binding.required_modifiers_down(prior_flags)
&& self.both_sides_went_down_together()
}
/// A system shortcut such as Command-Tab swallows its key's release, and
/// the stale key blocks the next press until `recover_stale_keys` has
/// sampled 100 ms of neutral keyboard. A press of the binding itself
/// resynchronizes at once, as a fresh press, when the keyboard reported no
/// modifiers after every tracked key went down, only the binding's
/// modifiers went down since, and both the tracked keys and every other
/// modifier read up now. A lock stays locked, so the press finishes it.
/// Returns why the press stays ignored.
fn resync_stale_press(&mut self, now: CaptureInstant) -> Option<&'static str> {
let binding_key = self.binding.key_code;
let stale = |code: &u16| Some(*code) != binding_key;
let has_stale = self.held.key_codes().any(stale);
match self.state {
State::Dirty if has_stale => {}
State::Dirty => return Some("waiting for a neutral keyboard"),
// Tracked keys block only modifier-only presses outside Dirty.
State::Idle { .. } | State::Locked if has_stale && binding_key.is_none() => {}
_ => return None,
}
let history = self.modifier_history;
// Typing after the switch is fine; only the stale keys' own presses count.
let neutral_since_down = |code: &u16| {
history
.neutral_at
.zip(self.held.key_down_at(*code))
.is_some_and(|(neutral, down)| neutral > down)
};
if !self
.held
.key_codes()
.filter(|code| stale(code))
.all(neutral_since_down)
{
return Some("no neutral keyboard since the tracked keys went down");
}
let binding_mask = modifier_mask(self.binding.modifiers);
if history.since_neutral & !binding_mask != 0 {
return Some("another modifier went down since the keyboard was neutral");
}
// Key repeats refresh each key's timestamp, so a silent keyboard key is latched.
let silent = |code: &u16| {
crate::app_settings::HotkeyKey::mouse_button(*code).is_none()
&& self.held.key_down_at(*code).is_some_and(|down| {
now.checked_duration_since(down)
.is_some_and(|age| age >= TRACKED_KEY_REPEAT_TIMEOUT)
})
};
let held: Vec<u16> = self
.held
.key_codes()
.filter(|code| stale(code))
.filter(|&&code| (self.physical.key_down)(code))
.copied()
.collect();
if !held.iter().all(silent) {
return Some("a tracked key is physically down");
} else if !held.is_empty() {
tracing::info!(key_codes = ?held, "tracked keys sent no key repeat; released");
}
// As in recovery, arrow keys can leave SecondaryFn set after release.
let mut others = HOTKEY_MODIFIERS_MASK & !binding_mask;
if self.function_modifier.1 == Some(false) {
others &= !crate::app_settings::FUNCTION_KEY_MASK;
}
if (self.physical.flags)() & others != 0 {
return Some("another modifier is physically down");
}
let key_codes: Vec<u16> = self.held.key_codes().copied().collect();
tracing::info!(
shortcut = "dictation",
key_codes = ?key_codes,
"resynchronized stale input tracking on a shortcut press"
);
self.held.retain_keys(|code| !stale(code));
// Command-Tab while hands-free: the press finishes the lock.
if !matches!(self.state, State::Locked) {
self.state = State::IDLE;
}
self.stale_keys_neutral_since = None;
// Edges stamped before the press must not finish or restart it, and a
// delayed older release must not untrack the binding's key. Edges at
// the press itself still count, so a same-tick chord discards it.
self.recovery_ignore_through = now.checked_sub(Duration::from_nanos(1));
self.recovery_updated_keys = binding_key
.filter(|code| self.held.contains_key(*code))
.into_iter()
.collect();
None
}
fn log_ignored_press(&self, reason: &'static str) {
let key_codes: Vec<u16> = self.held.key_codes().copied().collect();
tracing::info!(
shortcut = "dictation",
reason,
key_codes = ?key_codes,
"shortcut press ignored"
);
}
fn trigger_pressed(&self, event: InputEvent, fresh_key_down: bool, prior_flags: u64) -> bool {
match self.binding.key_code {
Some(key_code) => {
fresh_key_down
&& matches!(
event,
InputEvent::Key {
code,
down: true,
flags,
} if code == key_code && self.binding.exact_modifiers(flags)
)
}
None => self.held.keys_empty() && self.modifiers_went_down(event, prior_flags),
}
}
/// A modifier-only chord on both sides of a modifier (both Options)
/// presses only when its two keys go down within `BOTH_SIDES_WINDOW`,
/// in either order, as one gesture; one held and the other added later
/// presses nothing. Other chords press as soon as their keys are down.
fn both_sides_went_down_together(&self) -> bool {
self.binding.both_sides_masks().all(|(left, right)| {
match (self.held.side_down_at(left), self.held.side_down_at(right)) {
(Some(left), Some(right)) => {
left.max(right).duration_since(left.min(right)) <= BOTH_SIDES_WINDOW
}
_ => false,
}
})
}
}
/// Return and the keypad's Enter.
const RETURN_KEY_CODES: [u16; 2] = [36, 76];
fn is_return_key(event: InputEvent) -> bool {
matches!(
event,
InputEvent::Key { code, .. } | InputEvent::PhysicalKeyDown { code, .. }
if RETURN_KEY_CODES.contains(&code)
)
}
/// Each modifier key's device flag, left and right.
const MODIFIER_SIDE_MASKS: [u64; 8] = {
use crate::app_settings::{
LEFT_COMMAND_MASK, LEFT_CONTROL_MASK, LEFT_OPTION_MASK, LEFT_SHIFT_MASK,
RIGHT_COMMAND_MASK, RIGHT_CONTROL_MASK, RIGHT_OPTION_MASK, RIGHT_SHIFT_MASK,
};
[
LEFT_CONTROL_MASK,
RIGHT_CONTROL_MASK,
LEFT_OPTION_MASK,
RIGHT_OPTION_MASK,
LEFT_SHIFT_MASK,
RIGHT_SHIFT_MASK,
LEFT_COMMAND_MASK,
RIGHT_COMMAND_MASK,
]
};
/// The device-independent flag bits of `modifiers`, either side.
fn modifier_mask(modifiers: crate::app_settings::HotkeyModifiers) -> u64 {
use crate::app_settings::{
COMMAND_KEY_MASK, CONTROL_KEY_MASK, FUNCTION_KEY_MASK, OPTION_KEY_MASK, SHIFT_KEY_MASK,
};
[
(modifiers.control.is_some(), CONTROL_KEY_MASK),
(modifiers.option.is_some(), OPTION_KEY_MASK),
(modifiers.shift.is_some(), SHIFT_KEY_MASK),
(modifiers.command.is_some(), COMMAND_KEY_MASK),
(modifiers.function, FUNCTION_KEY_MASK),
]
.into_iter()
.filter(|(held, _)| *held)
.fold(0, |mask, (_, bit)| mask | bit)
}
fn trigger_is_physically_down(binding: RuntimeHotkey) -> bool {
// Unit tests never read the real keyboard: a key held while they run must
// not change their outcome, and the first HID query can deadlock in a
// process without a window-server session.
if cfg!(test) {
return false;
}
// HID state reflects the physical keyboard. Combined-session state can
// remain latched after synthetic events or application switching.
// SAFETY: These are pure system queries with a documented state id.
unsafe {
!binding.is_empty()
&& binding.exact_modifiers(CGEventSourceFlagsState(HID_SYSTEM_STATE))
&& binding
.key_code
.is_none_or(|code| input_is_down(HID_SYSTEM_STATE, code))
}
}
// The suppression tests drive the hotkey's private state, so they live under it.
#[cfg(test)]
#[path = "tests.rs"]
mod tests;