Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't repeat hotkeys on Windows #584

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions crates/livesplit-hotkey/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ struct State {
hook: HHOOK,
events: Sender<Hotkey>,
modifiers: Modifiers,
// FIXME: Use variant count when it's stable.
// https://github.com/rust-lang/rust/issues/73662
key_state: [u8; 256 / 8],
}

// This static assert ensures we have enough states to represent all key codes.
const _: () = assert!(mem::size_of::<KeyCode>() == 1);

thread_local! {
static STATE: RefCell<Option<State>> = RefCell::new(None);
}
Expand Down Expand Up @@ -276,28 +282,33 @@ unsafe extern "system" fn callback_proc(code: c_int, wparam: WPARAM, lparam: LPA
};

if let Some(key_code) = parse_scan_code(scan_code) {
state
.events
.send(Hotkey {
key_code,
modifiers: state.modifiers,
})
.expect("Callback Thread disconnected");

match key_code {
KeyCode::AltLeft | KeyCode::AltRight => {
state.modifiers.insert(Modifiers::ALT);
}
KeyCode::ControlLeft | KeyCode::ControlRight => {
state.modifiers.insert(Modifiers::CONTROL);
}
KeyCode::MetaLeft | KeyCode::MetaRight => {
state.modifiers.insert(Modifiers::META);
}
KeyCode::ShiftLeft | KeyCode::ShiftRight => {
state.modifiers.insert(Modifiers::SHIFT);
let (idx, bit) = key_idx(key_code);
if state.key_state[idx as usize] & bit == 0 {
state.key_state[idx as usize] |= bit;

state
.events
.send(Hotkey {
key_code,
modifiers: state.modifiers,
})
.expect("Callback Thread disconnected");

match key_code {
KeyCode::AltLeft | KeyCode::AltRight => {
state.modifiers.insert(Modifiers::ALT);
}
KeyCode::ControlLeft | KeyCode::ControlRight => {
state.modifiers.insert(Modifiers::CONTROL);
}
KeyCode::MetaLeft | KeyCode::MetaRight => {
state.modifiers.insert(Modifiers::META);
}
KeyCode::ShiftLeft | KeyCode::ShiftRight => {
state.modifiers.insert(Modifiers::SHIFT);
}
_ => {}
}
_ => {}
}
}
} else if event == WM_KEYUP || event == WM_SYSKEYUP {
Expand Down Expand Up @@ -326,6 +337,9 @@ unsafe extern "system" fn callback_proc(code: c_int, wparam: WPARAM, lparam: LPA
};

if let Some(key_code) = parse_scan_code(scan_code) {
let (idx, bit) = key_idx(key_code);
state.key_state[idx as usize] &= !bit;

match key_code {
KeyCode::AltLeft | KeyCode::AltRight => {
state.modifiers.remove(Modifiers::ALT);
Expand All @@ -349,6 +363,12 @@ unsafe extern "system" fn callback_proc(code: c_int, wparam: WPARAM, lparam: LPA
})
}

#[inline]
fn key_idx(key_code: KeyCode) -> (u8, u8) {
let value = key_code as u8;
(value / 8, 1 << (value % 8))
}

impl Hook {
/// Creates a new hook.
pub fn new() -> Result<Self> {
Expand Down Expand Up @@ -387,6 +407,7 @@ impl Hook {
hook,
events: events_tx,
modifiers: Modifiers::empty(),
key_state: Default::default(),
});

Ok(())
Expand Down