summaryrefslogtreecommitdiffstats
path: root/futures/src/keyboard.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-10 00:34:21 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-10 00:34:21 +0200
commitb8e5693a3089d728b4f8d4b3b0b7197202ebd732 (patch)
tree79a9f84f9920525657fbe03d53ce33bab09053d7 /futures/src/keyboard.rs
parent956512338905bac0b156fdaf16fe3c3e07e97a84 (diff)
parenta3489e4af960388e9f73988b88df361022a654a4 (diff)
downloadiced-b8e5693a3089d728b4f8d4b3b0b7197202ebd732.tar.gz
iced-b8e5693a3089d728b4f8d4b3b0b7197202ebd732.tar.bz2
iced-b8e5693a3089d728b4f8d4b3b0b7197202ebd732.zip
Merge branch 'master' into explicit-text-caching
Diffstat (limited to 'futures/src/keyboard.rs')
-rw-r--r--futures/src/keyboard.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/futures/src/keyboard.rs b/futures/src/keyboard.rs
new file mode 100644
index 00000000..af68e1f2
--- /dev/null
+++ b/futures/src/keyboard.rs
@@ -0,0 +1,61 @@
+//! Listen to keyboard events.
+use crate::core;
+use crate::core::keyboard::{Event, KeyCode, Modifiers};
+use crate::subscription::{self, Subscription};
+use crate::MaybeSend;
+
+/// Listens to keyboard key presses and calls the given function
+/// map them into actual messages.
+///
+/// If the function returns `None`, the key press will be simply
+/// ignored.
+pub fn on_key_press<Message>(
+ f: fn(KeyCode, Modifiers) -> Option<Message>,
+) -> Subscription<Message>
+where
+ Message: MaybeSend + 'static,
+{
+ #[derive(Hash)]
+ struct OnKeyPress;
+
+ subscription::filter_map((OnKeyPress, f), move |event, status| {
+ match (event, status) {
+ (
+ core::Event::Keyboard(Event::KeyPressed {
+ key_code,
+ modifiers,
+ }),
+ core::event::Status::Ignored,
+ ) => f(key_code, modifiers),
+ _ => None,
+ }
+ })
+}
+
+/// Listens to keyboard key releases and calls the given function
+/// map them into actual messages.
+///
+/// If the function returns `None`, the key release will be simply
+/// ignored.
+pub fn on_key_release<Message>(
+ f: fn(KeyCode, Modifiers) -> Option<Message>,
+) -> Subscription<Message>
+where
+ Message: MaybeSend + 'static,
+{
+ #[derive(Hash)]
+ struct OnKeyRelease;
+
+ subscription::filter_map((OnKeyRelease, f), move |event, status| {
+ match (event, status) {
+ (
+ core::Event::Keyboard(Event::KeyReleased {
+ key_code,
+ modifiers,
+ }),
+ core::event::Status::Ignored,
+ ) => f(key_code, modifiers),
+ _ => None,
+ }
+ })
+}