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
use super::EventControl;
use crate::core::NodeId;
#[derive(Clone)]
pub struct KeyboardEvent {
pub key: String,
pub code: String,
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub meta: bool,
pub target_id: NodeId,
/// Whether a text input holds keyboard focus when this event fires.
/// `on_key_down` handlers run before the focused input consumes the key,
/// so global shortcuts (e.g. an app's ctrl+z) should check this and step
/// aside while the user is typing — the input's own editing (including
/// its undo/redo) takes the key otherwise.
pub text_input_focused: bool,
/// Whether this key belongs to an input method (IME) composition, like the
/// DOM's `isComposing`.
///
/// The keys the input method takes never arrive as presses: Windows
/// reports them as `Process` (the `Enter` that confirms a composition
/// included) and macOS not at all, so `on_key_down` handlers and lurq's own
/// key handling do not see them. A press that does arrive while a
/// composition is shown, a key the input method passed on, has this set,
/// and lurq handles it as usual, except an `Enter`: no lurq default (form
/// submit, button or select activation, newline, blur) acts on it. A
/// handler that sends on `Enter` can skip it the same way. Releases of the
/// input method's keys reach `on_key_up` handlers with this set. It is also
/// set on the event a committed composition passes to `on_input` handlers
/// (`TextInputEvent::keyboard`), whose `key` is the committed text.
pub composing: bool,
pub(crate) control: EventControl,
}
impl KeyboardEvent {
pub fn new(
key: impl Into<String>,
code: impl Into<String>,
shift: bool,
ctrl: bool,
alt: bool,
meta: bool,
target_id: NodeId,
) -> Self {
Self {
key: key.into(),
code: code.into(),
shift,
ctrl,
alt,
meta,
target_id,
text_input_focused: false,
composing: false,
control: EventControl::new(),
}
}
pub fn prevent_default(&self) {
self.control.prevent_default();
}
pub fn default_prevented(&self) -> bool {
self.control.default_prevented()
}
pub fn stop_propagation(&self) {
self.control.stop_propagation();
}
pub fn propagation_stopped(&self) -> bool {
self.control.propagation_stopped()
}
pub fn stop_immediate_propagation(&self) {
self.control.stop_immediate_propagation();
}
pub fn immediate_propagation_stopped(&self) -> bool {
self.control.immediate_propagation_stopped()
}
}