Skip to content

Commit

Permalink
Introduce a single cross-platform KeyCode enum
Browse files Browse the repository at this point in the history
We used to have a different `KeyCode` enum on every platform making it
unnecessarily hard to specify hotkeys in a cross platform manner. Here
we merge them all together into a single `KeyCode` enum. It is based on
the [UI Events KeyboardEvent code
Values](https://www.w3.org/TR/uievents-code/) web standard. This also
meant we now had to map the values from the individual operating systems
to these values. For this we use the tables on [MSDN - KeyboardEvent:
code
values](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values).
There are a few additional and alternative values, so the `KeyCode` enum
and its parsing got extended a bit beyond the standard to handle these.
The implementation for Windows and Linux also slightly changed. Both of
these used to use Keyboard layout specific hotkeys, which isn't what we
want. We may however want to introduce a way to resolve the hotkeys to
the current Keyboard layout for visualization purposes in the future.
  • Loading branch information
CryZe committed Jul 26, 2021
1 parent d991984 commit 234066f
Show file tree
Hide file tree
Showing 13 changed files with 744 additions and 3,274 deletions.

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions crates/livesplit-hotkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,30 @@ cfg_if::cfg_if! {
pub use self::other::*;
}
}

mod key_code;
pub use self::key_code::*;

#[cfg(test)]
mod tests {
use std::{thread, time::Duration};

use super::*;

#[test]
fn test() {
let hook = Hook::new().unwrap();
hook.register(KeyCode::Numpad1, || println!("A")).unwrap();
println!("Press Numpad1");
thread::sleep(Duration::from_secs(5));
hook.unregister(KeyCode::Numpad1).unwrap();
hook.register(KeyCode::Numpad4, || println!("B")).unwrap();
println!("Press Numpad4");
thread::sleep(Duration::from_secs(5));
hook.unregister(KeyCode::Numpad4).unwrap();
hook.register(KeyCode::Numpad1, || println!("C")).unwrap();
println!("Press Numpad1");
thread::sleep(Duration::from_secs(5));
hook.unregister(KeyCode::Numpad1).unwrap();
}
}
Loading

0 comments on commit 234066f

Please sign in to comment.