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
use keyboard_types::{Code, Key, Location, Modifiers};
use typed_builder::TypedBuilder;
/// Describes a key's values, typically at the time of a key event
#[derive(TypedBuilder, Clone, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyProperties {
/// The effect of the key, commonly known as `key`
///
/// Takes into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout.
effect: Key,
/// The physical key being pressed, commonly known as `code`
///
/// The value is not affected by the current keyboard layout or modifier state, so a particular key will always return the same value. This also means that this does not directly correspond to the character typed by the key.
physical: Code,
/// The set of modifier keys held
modifiers: Modifiers,
/// The location of the physical key on the keyboard or other input device
location: Location,
/// `true` iff the key is being held down so that it is automatically repeating
is_auto_repeating: bool,
}
impl KeyProperties {
/// The effect of the key, commonly known as `key`
///
/// Takes into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout.
pub fn effect(&self) -> &Key {
&self.effect
}
/// The physical key being pressed, commonly known as `code`
///
/// The value is not affected by the current keyboard layout or modifier state, so a particular key will always return the same value. This also means that this does not directly correspond to the character typed by the key.
pub fn physical(&self) -> Code {
self.physical
}
/// The set of modifier keys held
pub fn modifiers(&self) -> Modifiers {
self.modifiers
}
/// The location of the physical key on the keyboard or other input device
pub fn location(&self) -> Location {
self.location
}
/// `true` iff the key is being held down so that it is automatically repeating
pub fn is_auto_repeating(&self) -> bool {
self.is_auto_repeating
}
}
#[cfg(test)]
mod key_properties {
use super::*;
use assert2::assert;
#[test]
fn builder() {
let k: KeyProperties = KeyProperties::builder()
.effect(Key::Character("R".to_string()))
.physical(Code::KeyR)
.modifiers(Modifiers::SHIFT)
.location(Location::default())
.is_auto_repeating(false)
.build();
assert!(k.effect() == &Key::Character("R".to_string()));
assert!(k.physical() == Code::KeyR);
assert!(k.modifiers() == Modifiers::SHIFT);
assert!(k.location() == Location::default());
assert!(k.is_auto_repeating() == false);
}
}