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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
pub use code::Code;
pub use key::Key;
pub use location::Location;
pub use modifiers::Modifiers;
pub use shortcuts::ShortcutMatcher;
#[macro_use]
extern crate bitflags;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
mod code;
mod key;
mod location;
mod modifiers;
mod shortcuts;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum KeyState {
Down,
Up,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct KeyboardEvent {
pub state: KeyState,
pub key: Key,
pub code: Code,
pub location: Location,
pub modifiers: Modifiers,
pub repeat: bool,
pub is_composing: bool,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CompositionState {
Start,
Update,
End,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompositionEvent {
pub state: CompositionState,
pub data: String,
}
impl Key {
pub fn legacy_keycode(&self) -> u32 {
match self {
Key::Backspace => 8,
Key::Tab => 9,
Key::Enter => 13,
Key::Shift => 16,
Key::Control => 17,
Key::Alt => 18,
Key::CapsLock => 20,
Key::Escape => 27,
Key::PageUp => 33,
Key::PageDown => 34,
Key::End => 35,
Key::Home => 36,
Key::ArrowLeft => 37,
Key::ArrowUp => 38,
Key::ArrowRight => 39,
Key::ArrowDown => 40,
Key::Delete => 46,
Key::Character(ref c) if c.len() == 1 => match c.chars().next().unwrap() {
' ' => 32,
x @'0'...'9' => x as u32,
x @ 'a'...'z' => x.to_ascii_uppercase() as u32,
x @ 'A'...'Z' => x as u32,
';' | ':' => 186,
'=' | '+' => 187,
',' | '<' => 188,
'-' | '_' => 189,
'.' | '>' => 190,
'/' | '?' => 191,
'`' | '~' => 192,
'[' | '{' => 219,
'\\' | '|' => 220,
']' | '}' => 221,
'\'' | '\"' => 222,
_ => 0,
},
_ => 0,
}
}
}