zng/
keyboard.rs

1//! Keyboard service, properties, events and other types.
2//!
3//! The example below defines a window that shows the pressed keys and prints the key state changes.
4//!
5//! ```
6//! use zng::prelude::*;
7//! # fn example() {
8//!
9//! # let _ =
10//! Window! {
11//!     child = Text!(keyboard::KEYBOARD.keys().map_debug(false));
12//!     keyboard::on_key_input = hn!(|args| {
13//!         println!("key {:?} {:?}", args.key, args.state);
14//!     });
15//! }
16//! # ; }
17//! ```
18//!
19//! Keyboard events are send to the focused widget, if there is no focused widget no event is send. You can
20//! subscribe directly to the [`KEY_INPUT_EVENT`] to monitor all keyboard events for any focused widget.
21//!
22//! # Full API
23//!
24//! See [`zng_ext_input::keyboard`] and [`zng_wgt_input::keyboard`] for the full keyboard API.
25//! See [`zng_app::view_process::raw_events`] for raw keyboard events that are processed to generate the key input event.
26
27pub use zng_app::shortcut::ModifiersState;
28
29pub use zng_ext_input::keyboard::{
30    HeadlessAppKeyboardExt, KEY_INPUT_EVENT, KEYBOARD, Key, KeyCode, KeyInputArgs, KeyLocation, KeyRepeatConfig, KeyState,
31    MODIFIERS_CHANGED_EVENT, ModifiersChangedArgs, NativeKeyCode,
32};
33
34pub use zng_wgt_input::keyboard::{
35    on_disabled_key_input, on_key_down, on_key_input, on_key_up, on_pre_disabled_key_input, on_pre_key_down, on_pre_key_input,
36    on_pre_key_up,
37};
38
39/// Raw keyboard hardware events, received independent of what window or widget is focused.
40///
41/// You must enable device events in the app to receive this events.
42///
43/// ```no_run
44/// use zng::app::raw_device_events::InputDeviceCapability;
45/// use zng::prelude::*;
46///
47/// APP.defaults().run_window(async {
48///     APP.device_events_filter().modify(|f| f.input |= InputDeviceCapability::KEY);
49///
50///     keyboard::raw_device_events::KEY_EVENT
51///         .on_pre_event(hn!(|args| {
52///             if args.state == keyboard::KeyState::Pressed {
53///                 println!("key pressed {:?}", args.key_code);
54///             }
55///         }))
56///         .perm();
57///
58///     Window!()
59/// });
60/// ```
61pub mod raw_device_events {
62    pub use zng_app::view_process::raw_device_events::{KEY_EVENT, KeyArgs};
63}