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//! # let _scope = APP.defaults();
8//!
9//! # let _ =
10//! Window! {
11//!     child = Text!(keyboard::KEYBOARD.keys().map_debug());
12//!     keyboard::on_key_input = hn!(|args: &keyboard::KeyInputArgs| {
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::prelude::*;
45///
46/// APP.defaults().enable_device_events().run_window(async {
47///     keyboard::raw_device_events::KEY_EVENT.on_pre_event(app_hn!(|args: &keyboard::raw_device_events::KeyArgs, _| {
48///         if args.state == keyboard::KeyState::Pressed {
49///             println!("key pressed {:?}", args.key_code);
50///         }
51///     })).perm();
52///
53///     Window!()
54/// });
55/// ```
56pub mod raw_device_events {
57    pub use zng_app::view_process::raw_device_events::{KEY_EVENT, KeyArgs, TEXT_EVENT, TextArgs};
58}