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
//! Keyboard events, [`on_key_down`](fn@on_key_down), [`on_key_up`](fn@on_key_up) and more.
//!
//! These events are low level and directly tied to a keyboard device.
//! Before using them review the [`gesture`](super::gesture) properties, in particular
//! the [`click_shortcut`](fn@super::gesture::click_shortcut) property.

use zng_ext_input::keyboard::{KeyInputArgs, KeyState, KEY_INPUT_EVENT};
use zng_wgt::prelude::*;

event_property! {
    /// Event fired when a keyboard key is pressed or released and the widget is enabled.
    ///
    /// # Route
    ///
    /// The event is raised in the [keyboard focused](crate::properties::is_focused)
    /// widget and then each parent up to the root. If propagation stop
    /// is requested the event is not notified further. If the widget is disabled or blocked the event is not notified.
    ///
    /// This route is also called *bubbling*.
    ///
    /// # Keys
    ///
    /// Any key press/release generates a key input event, including keys codes that don't map
    /// to any virtual key, see [`KeyInputArgs`] for more details.
    ///
    /// For key combinations consider using a [`click_shortcut`] with a click handler.
    ///
    /// [`click_shortcut`]: fn@crate::gesture::click_shortcut
    ///
    /// # Underlying Event
    ///
    /// This event property uses the [`KEY_INPUT_EVENT`] that is included in the default app.
    ///
    /// [`KeyInputArgs`]: zng_ext_input::keyboard::KeyInputArgs
    /// [`KEY_INPUT_EVENT`]: zng_ext_input::keyboard::KEY_INPUT_EVENT
    pub fn key_input {
        event: KEY_INPUT_EVENT,
        args: KeyInputArgs,
        filter: |args| args.is_enabled(WIDGET.id()),
    }

    /// Event fired when a keyboard key is pressed or released and the widget is disabled.
    ///
    /// # Route
    ///
    /// The event is raised in the [keyboard focused](crate::properties::is_focused)
    /// widget and then each parent up to the root. If propagation stop
    /// is requested the event is not notified further. If the widget is enabled or blocked the event is not notified.
    ///
    /// This route is also called *bubbling*.
    ///
    /// # Keys
    ///
    /// Any key press/release generates a key input event, including keys that don't map
    /// to any virtual key, see [`KeyInputArgs`] for more details.
    ///
    /// For key combinations consider using a [`click_shortcut`] with a click handler.
    ///
    /// [`click_shortcut`]: fn@crate::gesture::click_shortcut
    ///
    /// # Underlying Event
    ///
    /// This event property uses the [`KEY_INPUT_EVENT`] that is included in the default app.
    ///
    /// [`KeyInputArgs`]: zng_ext_input::keyboard::KeyInputArgs
    /// [`KEY_INPUT_EVENT`]: zng_ext_input::keyboard::KEY_INPUT_EVENT
    pub fn disabled_key_input {
        event: KEY_INPUT_EVENT,
        args: KeyInputArgs,
        filter: |args| args.is_disabled(WIDGET.id()),
    }

    /// Event fired when a keyboard key is pressed and the widget is enabled.
    ///
    /// # Route
    ///
    /// The event is raised in the [keyboard focused](crate::properties::is_focused)
    /// widget and then each parent up to the root. If propagation stop
    /// is requested the event is not notified further. If the widget is disabled or blocked the event is not notified.
    ///
    /// This route is also called *bubbling*.
    ///
    /// # Keys
    ///
    /// Any key press generates a key down event, including keys that don't map to any virtual key, see [`KeyInputArgs`]
    /// for more details.
    ///
    /// For key combinations consider using a [`click_shortcut`] with a click handler.
    ///
    /// [`click_shortcut`]: fn@crate::gesture::click_shortcut
    ///
    /// # Underlying Event
    ///
    /// This event property uses the [`KEY_INPUT_EVENT`] that is included in the default app.
    ///
    /// [`KeyInputArgs`]: zng_ext_input::keyboard::KeyInputArgs
    /// [`KEY_INPUT_EVENT`]: zng_ext_input::keyboard::KEY_INPUT_EVENT
    pub fn key_down {
        event: KEY_INPUT_EVENT,
        args: KeyInputArgs,
        filter: |args| args.state == KeyState::Pressed && args.is_enabled(WIDGET.id()),
    }

    /// Event fired when a keyboard key is released and the widget is enabled.
    ///
    /// # Route
    ///
    /// The event is raised in the [keyboard focused](crate::properties::is_focused)
    /// widget and then each parent up to the root. If propagation stop
    /// is requested the event is not notified further. If the widget is disabled or blocked the event is not notified.
    ///
    /// This route is also called *bubbling*.
    ///
    /// # Keys
    ///
    /// Any key release generates a key up event, including keys that don't map to any virtual key, see [`KeyInputArgs`]
    /// for more details.
    /// For key combinations consider using a [`click_shortcut`] with a click handler.
    ///
    /// [`click_shortcut`]: fn@crate::gesture::click_shortcut
    ///
    /// # Underlying Event
    ///
    /// This event property uses the [`KEY_INPUT_EVENT`] that is included in the default app.
    ///
    /// [`KeyInputArgs`]: zng_ext_input::keyboard::KeyInputArgs
    /// [`KEY_INPUT_EVENT`]: zng_ext_input::keyboard::KEY_INPUT_EVENT
    pub fn key_up {
        event: KEY_INPUT_EVENT,
        args: KeyInputArgs,
        filter: |args| args.state == KeyState::Released && args.is_enabled(WIDGET.id()),
    }
}