Merge pull request #3135 from edwloef/subscribe_key_code
Add `physical_key` to key subscriptions
This commit is contained in:
commit
85d2d1a6d4
10 changed files with 85 additions and 86 deletions
|
|
@ -65,7 +65,7 @@ impl Key {
|
|||
let mut chars = s.chars();
|
||||
let c = chars.next()?;
|
||||
|
||||
if chars.next().is_none() && c <= '\u{ff}' {
|
||||
if chars.next().is_none() && c < '\u{370}' {
|
||||
return Some(c);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -381,11 +381,13 @@ where
|
|||
program.subscription(&self.state).map(Event::Program);
|
||||
debug::subscriptions_tracked(subscription.units());
|
||||
|
||||
let hotkeys =
|
||||
futures::keyboard::on_key_press(|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);
|
||||
|
|
|
|||
|
|
@ -58,12 +58,25 @@ impl Layout {
|
|||
fn subscription(&self) -> Subscription<Message> {
|
||||
use keyboard::key;
|
||||
|
||||
keyboard::on_key_release(|key, _modifiers| match key {
|
||||
keyboard::Key::Named(key::Named::ArrowLeft) => {
|
||||
Some(Message::Previous)
|
||||
keyboard::listen().filter_map(|event| {
|
||||
let keyboard::Event::KeyPressed {
|
||||
modified_key,
|
||||
repeat: false,
|
||||
..
|
||||
} = 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,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -117,12 +117,17 @@ impl Example {
|
|||
}
|
||||
|
||||
fn subscription(&self) -> Subscription<Message> {
|
||||
keyboard::on_key_press(|key_code, modifiers| {
|
||||
keyboard::listen().filter_map(|event| {
|
||||
let keyboard::Event::KeyPressed { key, modifiers, .. } = event
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if !modifiers.command() {
|
||||
return None;
|
||||
}
|
||||
|
||||
handle_hotkey(key_code)
|
||||
handle_hotkey(key)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -227,8 +227,12 @@ impl Example {
|
|||
fn subscription(&self) -> Subscription<Message> {
|
||||
use keyboard::key;
|
||||
|
||||
keyboard::on_key_press(|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
|
||||
|
|
|
|||
|
|
@ -65,13 +65,14 @@ impl Stopwatch {
|
|||
}
|
||||
};
|
||||
|
||||
fn handle_hotkey(
|
||||
key: keyboard::Key,
|
||||
_modifiers: keyboard::Modifiers,
|
||||
) -> Option<Message> {
|
||||
fn handle_hotkey(event: keyboard::Event) -> Option<Message> {
|
||||
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)
|
||||
}
|
||||
|
|
@ -80,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> {
|
||||
|
|
|
|||
|
|
@ -190,18 +190,26 @@ impl Styling {
|
|||
}
|
||||
|
||||
fn subscription(&self) -> Subscription<Message> {
|
||||
keyboard::on_key_press(|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),
|
||||
repeat: false,
|
||||
..
|
||||
} = 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,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -254,12 +254,12 @@ impl Todos {
|
|||
fn subscription(&self) -> Subscription<Message> {
|
||||
use keyboard::key;
|
||||
|
||||
keyboard::on_key_press(|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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,56 +1,18 @@
|
|||
//! Listen to keyboard events.
|
||||
use crate::MaybeSend;
|
||||
use crate::core;
|
||||
use crate::core::event;
|
||||
use crate::core::keyboard::{Event, Key, Modifiers};
|
||||
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<Message>(
|
||||
f: fn(Key, Modifiers) -> Option<Message>,
|
||||
) -> Subscription<Message>
|
||||
where
|
||||
Message: MaybeSend + 'static,
|
||||
{
|
||||
/// Returns a [`Subscription`] that listens to ignored keyboard events.
|
||||
pub fn listen() -> Subscription<Event> {
|
||||
#[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, modifiers, .. }),
|
||||
status: event::Status::Ignored,
|
||||
event: core::Event::Keyboard(event),
|
||||
..
|
||||
} => f(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<Message>(
|
||||
f: fn(Key, Modifiers) -> Option<Message>,
|
||||
) -> Subscription<Message>
|
||||
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, modifiers, .. }),
|
||||
status: event::Status::Ignored,
|
||||
..
|
||||
} => f(key, modifiers),
|
||||
} => Some(event),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -368,8 +368,8 @@
|
|||
//! visible widgets of your user interface, at every moment.
|
||||
//!
|
||||
//! As with tasks, some modules expose convenient functions that build a [`Subscription`] for you—like
|
||||
//! [`time::every`] which can be used to listen to time, or [`keyboard::on_key_press`] which will notify you
|
||||
//! of any key presses. But you can also create your own with [`Subscription::run`] and [`run_with`].
|
||||
//! [`time::every`] which can be used to listen to time, or [`keyboard::listen`] which will notify you
|
||||
//! of any keyboard events. But you can also create your own with [`Subscription::run`] and [`run_with`].
|
||||
//!
|
||||
//! [`run_with`]: Subscription::run_with
|
||||
//!
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue