1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#[allow(unused_imports)]
use crate::{evdev, routine::Routine};
use kbvm_proc::CloneWithDelta;
/// A keycode.
///
/// Keycodes represent physical keys. On Linux, they usually correspond to evdev
/// events which are in turn modeled after the USB standard. The [`evdev`] module
/// contains constants for evdev keycodes.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, CloneWithDelta, Default)]
pub struct Keycode(pub(crate) u32);
impl Keycode {
/// Returns the raw `u32` representing this keycode.
///
/// This value should only be used in [`Routine`]s.
pub const fn raw(self) -> u32 {
self.0
}
/// Creates a keycode from an X11 keycode.
#[inline]
pub const fn from_x11(kc: u32) -> Self {
Self(kc)
}
/// Converts the keycode to an X11 keycode.
///
/// If this keycode was not created via [`Keycode::from_x11`], then the conversion is
/// performed on a best-effort basis.
#[inline]
pub const fn to_x11(self) -> u32 {
self.0
}
/// Creates a keycode from an evdev code.
#[inline]
pub const fn from_evdev(kc: u32) -> Self {
Self(kc.saturating_add(8))
}
/// Converts the keycode to an evdev code.
///
/// If this keycode was not created via [`Keycode::from_evdev`], then the conversion
/// is performed on a best-effort basis.
#[inline]
pub const fn to_evdev(self) -> u32 {
self.0.saturating_sub(8)
}
}