fantoccini/
key.rs

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Key codes for use with Actions.

use std::fmt::{Display, Formatter};
use std::ops::{Add, AddAssign};

/// Key codes for use with Actions.
#[derive(Debug, Clone, Copy)]
pub enum Key {
    /// Null
    Null,
    /// Cancel
    Cancel,
    /// Help
    Help,
    /// Backspace key
    Backspace,
    /// Tab key
    Tab,
    /// Clear
    Clear,
    /// Return key
    Return,
    /// Enter key
    Enter,
    /// Shift key
    Shift,
    /// Control key
    Control,
    /// Alt key
    Alt,
    /// Pause key
    Pause,
    /// Escape key
    Escape,
    /// Space bar
    Space,
    /// Page Up key
    PageUp,
    /// Page Down key
    PageDown,
    /// End key
    End,
    /// Home key
    Home,
    /// Left arrow key
    Left,
    /// Up arrow key
    Up,
    /// Right arrow key
    Right,
    /// Down arrow key
    Down,
    /// Insert key
    Insert,
    /// Delete key
    Delete,
    /// Semicolon key
    Semicolon,
    /// Equals key
    Equals,
    /// Numpad 0 key
    NumPad0,
    /// Numpad 1 key
    NumPad1,
    /// Numpad 2 key
    NumPad2,
    /// Numpad 3 key
    NumPad3,
    /// Numpad 4 key
    NumPad4,
    /// Numpad 5 key
    NumPad5,
    /// Numpad 6 key
    NumPad6,
    /// Numpad 7 key
    NumPad7,
    /// Numpad 8 key
    NumPad8,
    /// Numpad 9 key
    NumPad9,
    /// Multiply key
    Multiply,
    /// Add key
    Add,
    /// Separator key
    Separator,
    /// Subtract key
    Subtract,
    /// Decimal key
    Decimal,
    /// Divide key
    Divide,
    /// F1 key
    F1,
    /// F2 key
    F2,
    /// F3 key
    F3,
    /// F4 key
    F4,
    /// F5 key
    F5,
    /// F6 key
    F6,
    /// F7 key
    F7,
    /// F8 key
    F8,
    /// F9 key
    F9,
    /// F10 key
    F10,
    /// F11 key
    F11,
    /// F12 key
    F12,
    /// Meta key
    Meta,
    /// Command key
    Command,
}

impl From<Key> for char {
    fn from(k: Key) -> char {
        match k {
            Key::Null => '\u{e000}',
            Key::Cancel => '\u{e001}',
            Key::Help => '\u{e002}',
            Key::Backspace => '\u{e003}',
            Key::Tab => '\u{e004}',
            Key::Clear => '\u{e005}',
            Key::Return => '\u{e006}',
            Key::Enter => '\u{e007}',
            Key::Shift => '\u{e008}',
            Key::Control => '\u{e009}',
            Key::Alt => '\u{e00a}',
            Key::Pause => '\u{e00b}',
            Key::Escape => '\u{e00c}',
            Key::Space => '\u{e00d}',
            Key::PageUp => '\u{e00e}',
            Key::PageDown => '\u{e00f}',
            Key::End => '\u{e010}',
            Key::Home => '\u{e011}',
            Key::Left => '\u{e012}',
            Key::Up => '\u{e013}',
            Key::Right => '\u{e014}',
            Key::Down => '\u{e015}',
            Key::Insert => '\u{e016}',
            Key::Delete => '\u{e017}',
            Key::Semicolon => '\u{e018}',
            Key::Equals => '\u{e019}',
            Key::NumPad0 => '\u{e01a}',
            Key::NumPad1 => '\u{e01b}',
            Key::NumPad2 => '\u{e01c}',
            Key::NumPad3 => '\u{e01d}',
            Key::NumPad4 => '\u{e01e}',
            Key::NumPad5 => '\u{e01f}',
            Key::NumPad6 => '\u{e020}',
            Key::NumPad7 => '\u{e021}',
            Key::NumPad8 => '\u{e022}',
            Key::NumPad9 => '\u{e023}',
            Key::Multiply => '\u{e024}',
            Key::Add => '\u{e025}',
            Key::Separator => '\u{e026}',
            Key::Subtract => '\u{e027}',
            Key::Decimal => '\u{e028}',
            Key::Divide => '\u{e029}',
            Key::F1 => '\u{e031}',
            Key::F2 => '\u{e032}',
            Key::F3 => '\u{e033}',
            Key::F4 => '\u{e034}',
            Key::F5 => '\u{e035}',
            Key::F6 => '\u{e036}',
            Key::F7 => '\u{e037}',
            Key::F8 => '\u{e038}',
            Key::F9 => '\u{e039}',
            Key::F10 => '\u{e03a}',
            Key::F11 => '\u{e03b}',
            Key::F12 => '\u{e03c}',
            Key::Meta => '\u{e03d}',
            Key::Command => '\u{e03d}',
        }
    }
}

impl Display for Key {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", char::from(*self))
    }
}

impl Add<Key> for String {
    type Output = String;

    fn add(mut self, rhs: Key) -> Self::Output {
        self.push(rhs.into());
        self
    }
}

impl AddAssign<Key> for String {
    fn add_assign(&mut self, rhs: Key) {
        self.push(rhs.into());
    }
}

impl Add<String> for Key {
    type Output = String;

    fn add(self, rhs: String) -> Self::Output {
        format!("{}{}", char::from(self), rhs)
    }
}

impl Add<Key> for Key {
    type Output = String;

    fn add(self, rhs: Key) -> Self::Output {
        format!("{}{}", char::from(self), char::from(rhs))
    }
}

impl Add<&str> for Key {
    type Output = String;

    fn add(self, rhs: &str) -> Self::Output {
        format!("{}{}", char::from(self), rhs)
    }
}

impl Add<Key> for &str {
    type Output = String;

    fn add(self, rhs: Key) -> Self::Output {
        format!("{}{}", self, char::from(rhs))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_keys() {
        assert_eq!(Key::Control + Key::Shift, "\u{e009}\u{e008}".to_string());
    }

    #[test]
    fn test_key_str() {
        assert_eq!(Key::Control + "a", "\u{e009}a".to_string());
        assert_eq!("a" + Key::Control, "a\u{e009}".to_string());
    }

    #[test]
    fn test_key_string() {
        assert_eq!(Key::Control + "a".to_string(), "\u{e009}a".to_string());
        assert_eq!("a".to_string() + Key::Control, "a\u{e009}".to_string());
    }

    #[test]
    fn test_string_addassign() {
        let mut k = String::new();
        k += Key::Control;
        assert_eq!(k, "\u{e009}".to_string());

        let mut k = "test".to_string();
        k += Key::Control;
        assert_eq!(k, "test\u{e009}".to_string());
    }

    #[test]
    fn test_key_key_string() {
        assert_eq!(
            Key::Control + Key::Alt + "e",
            "\u{e009}\u{e00a}e".to_string()
        );
    }
}