diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs index 534103c5..4c1271cf 100644 --- a/devtools/src/lib.rs +++ b/devtools/src/lib.rs @@ -381,15 +381,16 @@ where program.subscription(&self.state).map(Event::Program); debug::subscriptions_tracked(subscription.units()); - let hotkeys = futures::keyboard::on_key_press( - |key, _physical_key, _modifiers| match key { - keyboard::Key::Named(keyboard::key::Named::F12) => { - Some(Message::ToggleComet) - } + let hotkeys = futures::keyboard::listen() + .filter_map(|event| match event { + keyboard::Event::KeyPressed { + modified_key: + keyboard::Key::Named(keyboard::key::Named::F12), + .. + } => Some(Message::ToggleComet), _ => None, - }, - ) - .map(Event::Message); + }) + .map(Event::Message); let commands = debug::commands().map(Event::Command); diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index eae9228b..fb411083 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -58,12 +58,20 @@ impl Layout { fn subscription(&self) -> Subscription { use keyboard::key; - keyboard::on_key_release(|key, _physical_key, _modifiers| match key { - keyboard::Key::Named(key::Named::ArrowLeft) => { - Some(Message::Previous) + keyboard::listen().filter_map(|event| { + let keyboard::Event::KeyPressed { modified_key, .. } = event else { + return None; + }; + + match modified_key { + keyboard::Key::Named(key::Named::ArrowLeft) => { + Some(Message::Previous) + } + keyboard::Key::Named(key::Named::ArrowRight) => { + Some(Message::Next) + } + _ => None, } - keyboard::Key::Named(key::Named::ArrowRight) => Some(Message::Next), - _ => None, }) } diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 63e3470d..0c0f7d7c 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -117,7 +117,12 @@ impl Example { } fn subscription(&self) -> Subscription { - keyboard::on_key_press(|key, _physical_key, modifiers| { + keyboard::listen().filter_map(|event| { + let keyboard::Event::KeyPressed { key, modifiers, .. } = event + else { + return None; + }; + if !modifiers.command() { return None; } diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 716706c6..b99157ea 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -227,8 +227,12 @@ impl Example { fn subscription(&self) -> Subscription { use keyboard::key; - keyboard::on_key_press(|key, _physical_key, _modifiers| { - if let keyboard::Key::Named(key::Named::F5) = key { + keyboard::listen().filter_map(|event| { + if let keyboard::Event::KeyPressed { + modified_key: keyboard::Key::Named(key::Named::F5), + .. + } = event + { Some(Message::Screenshot) } else { None diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index d4023f0b..c3beb338 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -65,14 +65,14 @@ impl Stopwatch { } }; - fn handle_hotkey( - key: keyboard::Key, - _physical_key: keyboard::key::Physical, - _modifiers: keyboard::Modifiers, - ) -> Option { + fn handle_hotkey(event: keyboard::Event) -> Option { use keyboard::key; - match key.as_ref() { + let keyboard::Event::KeyPressed { modified_key, .. } = event else { + return None; + }; + + match modified_key.as_ref() { keyboard::Key::Named(key::Named::Space) => { Some(Message::Toggle) } @@ -81,7 +81,10 @@ impl Stopwatch { } } - Subscription::batch(vec![tick, keyboard::on_key_press(handle_hotkey)]) + Subscription::batch(vec![ + tick, + keyboard::listen().filter_map(handle_hotkey), + ]) } fn view(&self) -> Element<'_, Message> { diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index b1339021..6a655cde 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -190,18 +190,25 @@ impl Styling { } fn subscription(&self) -> Subscription { - keyboard::on_key_press(|key, _physical_key, _modifiers| match key { - keyboard::Key::Named( - keyboard::key::Named::ArrowUp | keyboard::key::Named::ArrowLeft, - ) => Some(Message::PreviousTheme), - keyboard::Key::Named( + keyboard::listen().filter_map(|event| { + let keyboard::Event::KeyPressed { + modified_key: keyboard::Key::Named(modified_key), + .. + } = event + else { + return None; + }; + + match modified_key { + keyboard::key::Named::ArrowUp + | keyboard::key::Named::ArrowLeft => { + Some(Message::PreviousTheme) + } keyboard::key::Named::ArrowDown - | keyboard::key::Named::ArrowRight, - ) => Some(Message::NextTheme), - keyboard::Key::Named(keyboard::key::Named::Space) => { - Some(Message::ClearTheme) + | keyboard::key::Named::ArrowRight => Some(Message::NextTheme), + keyboard::key::Named::Space => Some(Message::ClearTheme), + _ => None, } - _ => None, }) } diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 6f027fa0..34f86a91 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -254,12 +254,12 @@ impl Todos { fn subscription(&self) -> Subscription { use keyboard::key; - keyboard::on_key_press(|key, _physical_key, modifiers| { - let keyboard::Key::Named(key) = key else { - return None; - }; - - match (key, modifiers) { + keyboard::listen().filter_map(|event| match event { + keyboard::Event::KeyPressed { + key: keyboard::Key::Named(key), + modifiers, + .. + } => match (key, modifiers) { (key::Named::Tab, _) => Some(Message::TabPressed { shift: modifiers.shift(), }), @@ -270,7 +270,8 @@ impl Todos { Some(Message::ToggleFullscreen(window::Mode::Windowed)) } _ => None, - } + }, + _ => None, }) } } diff --git a/futures/src/keyboard.rs b/futures/src/keyboard.rs index f82ff2ed..06e7a9db 100644 --- a/futures/src/keyboard.rs +++ b/futures/src/keyboard.rs @@ -1,66 +1,18 @@ //! Listen to keyboard events. -use crate::MaybeSend; use crate::core; -use crate::core::event; -use crate::core::keyboard::{Event, Key, Modifiers, key}; +use crate::core::keyboard::Event; use crate::subscription::{self, Subscription}; -/// Listens to keyboard key presses and calls the given function -/// to map them into actual messages. -/// -/// If the function returns `None`, the key press will be simply -/// ignored. -pub fn on_key_press( - f: fn(Key, key::Physical, Modifiers) -> Option, -) -> Subscription -where - Message: MaybeSend + 'static, -{ +/// Returns a [`Subscription`] that listens to ignored keyboard events. +pub fn listen() -> Subscription { #[derive(Hash)] - struct OnKeyPress; + struct Listen; - subscription::filter_map((OnKeyPress, f), move |event| match event { + subscription::filter_map(Listen, move |event| match event { subscription::Event::Interaction { - event: - core::Event::Keyboard(Event::KeyPressed { - key, - physical_key, - modifiers, - .. - }), - status: event::Status::Ignored, + event: core::Event::Keyboard(event), .. - } => f(key, physical_key, modifiers), - _ => None, - }) -} - -/// Listens to keyboard key releases and calls the given function -/// to map them into actual messages. -/// -/// If the function returns `None`, the key release will be simply -/// ignored. -pub fn on_key_release( - f: fn(Key, key::Physical, Modifiers) -> Option, -) -> Subscription -where - Message: MaybeSend + 'static, -{ - #[derive(Hash)] - struct OnKeyRelease; - - subscription::filter_map((OnKeyRelease, f), move |event| match event { - subscription::Event::Interaction { - event: - core::Event::Keyboard(Event::KeyReleased { - key, - physical_key, - modifiers, - .. - }), - status: event::Status::Ignored, - .. - } => f(key, physical_key, modifiers), + } => Some(event), _ => None, }) } diff --git a/src/lib.rs b/src/lib.rs index 111c25e7..96cc009d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -590,7 +590,7 @@ pub mod keyboard { //! Listen and react to keyboard events. pub use crate::core::keyboard::key; pub use crate::core::keyboard::{Event, Key, Location, Modifiers}; - pub use iced_futures::keyboard::{on_key_press, on_key_release}; + pub use iced_futures::keyboard::listen; } pub mod mouse {