pub struct KeyInput { /* private fields */ }Expand description
Single key input by pressing a key and modifiers.
This struct is equivalent to a key combination in the syntax document such as “Ctrl+x”.
Implementations§
Source§impl KeyInput
impl KeyInput
Sourcepub fn new<K, M>(key: K, mods: M) -> Self
pub fn new<K, M>(key: K, mods: M) -> Self
Create a new KeyInput instance with checking the Shift modifier restriction described in the
syntax document.
use keybinds::{KeyInput, Key, Mods};
let k = KeyInput::new('x', Mods::CTRL);
assert_eq!(k.key(), Key::Char('x'));
assert_eq!(k.mods(), Mods::CTRL);
let k = KeyInput::new(Key::Enter, Mods::MOD);
assert_eq!(k.key(), Key::Enter);
assert_eq!(k.mods(), Mods::MOD);
// Shift modifier is removed when it is not used with named keys following the restriction.
let k = KeyInput::new('x', Mods::SHIFT | Mods::CTRL);
assert_eq!(k.key(), Key::Char('x'));
assert_eq!(k.mods(), Mods::CTRL);
// You need to use the following instead.
let k = KeyInput::new('X', Mods::CTRL);
assert_eq!(k.key(), Key::Char('X'));
assert_eq!(k.mods(), Mods::CTRL);Sourcepub fn key(&self) -> Key
pub fn key(&self) -> Key
Return the Key of the input.
Examples found in repository?
examples/vim.rs (line 401)
396 fn convert_key_input(&self, input: KeyInput) -> Option<Input> {
397 if self.mode != Mode::Insert {
398 return None;
399 }
400
401 let key = match input.key() {
402 keybinds::Key::Char(c) => Key::Char(c),
403 keybinds::Key::Copy => Key::Copy,
404 keybinds::Key::Cut => Key::Cut,
405 keybinds::Key::Paste => Key::Paste,
406 keybinds::Key::Backspace => Key::Backspace,
407 keybinds::Key::Delete => Key::Delete,
408 keybinds::Key::Enter => Key::Enter,
409 keybinds::Key::Up => Key::Up,
410 keybinds::Key::Right => Key::Right,
411 keybinds::Key::Down => Key::Down,
412 keybinds::Key::Left => Key::Left,
413 keybinds::Key::Home => Key::Home,
414 keybinds::Key::End => Key::End,
415 keybinds::Key::PageUp => Key::PageUp,
416 keybinds::Key::PageDown => Key::PageDown,
417 keybinds::Key::Tab => Key::Tab,
418 _ => return None,
419 };
420
421 let mods = input.mods();
422 Some(Input {
423 key,
424 ctrl: mods.contains(Mods::CTRL),
425 alt: mods.contains(Mods::ALT),
426 shift: mods.contains(Mods::SHIFT),
427 })
428 }More examples
examples/winit.rs (line 52)
49 fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
50 // Convert the window event into key input
51 let input = self.converter.convert(&event);
52 if input.key() != Key::Ignored {
53 println!("Key input: {input:?}");
54 }
55
56 // Check if the converted key input dispatches some action
57 if let Some(action) = self.keybinds.dispatch(input) {
58 println!("Action: {action:?}");
59
60 match action {
61 Action::SayHi => println!("Hi!"),
62 Action::ToggleMaximized => {
63 let window = self.window.as_ref().unwrap();
64 window.set_maximized(!window.is_maximized());
65 }
66 Action::ToggleTheme => {
67 let window = self.window.as_ref().unwrap();
68 let theme = match window.theme() {
69 Some(Theme::Dark) => Theme::Light,
70 _ => Theme::Dark,
71 };
72 window.set_theme(Some(theme));
73 }
74 Action::Exit => event_loop.exit(),
75 }
76 }
77
78 if let WindowEvent::CloseRequested = event {
79 event_loop.exit();
80 }
81 }examples/iced.rs (line 67)
62 fn update(&mut self, message: Message) -> Task<Message> {
63 match message {
64 Message::WindowOpen(id) => self.window_id = id,
65 Message::KeyEvent(event) => {
66 let input = KeyInput::from(&event);
67 if input.key() != Key::Ignored {
68 println!("Key input: {input:?}");
69 if !self.keybinds.is_ongoing() {
70 self.last_action.clear();
71 self.ongoing_input.clear();
72 }
73 if !self.ongoing_input.is_empty() {
74 self.ongoing_input.push_str(" → ");
75 }
76 write!(self.ongoing_input, "{input:}").unwrap();
77 }
78
79 if let Some(action) = self.keybinds.dispatch(event) {
80 self.last_action = format!("{action:?}");
81
82 // Handle the dispatched action
83 match action {
84 Action::SayHello => println!("Hello!"),
85 Action::ToggleMaximize => {
86 self.maximized = !self.maximized;
87 return window::maximize(self.window_id, self.maximized);
88 }
89 Action::ToggleTheme => {
90 self.theme = match self.theme {
91 Theme::Dark => Theme::Light,
92 _ => Theme::Dark,
93 };
94 }
95 Action::Exit => return iced::exit(),
96 }
97 }
98 }
99 Message::Reset => {
100 self.keybinds.reset();
101 self.ongoing_input.clear();
102 self.last_action.clear();
103 }
104 }
105 Task::none()
106 }Sourcepub fn mods(&self) -> Mods
pub fn mods(&self) -> Mods
Return the Mods of the input.
Examples found in repository?
examples/vim.rs (line 421)
396 fn convert_key_input(&self, input: KeyInput) -> Option<Input> {
397 if self.mode != Mode::Insert {
398 return None;
399 }
400
401 let key = match input.key() {
402 keybinds::Key::Char(c) => Key::Char(c),
403 keybinds::Key::Copy => Key::Copy,
404 keybinds::Key::Cut => Key::Cut,
405 keybinds::Key::Paste => Key::Paste,
406 keybinds::Key::Backspace => Key::Backspace,
407 keybinds::Key::Delete => Key::Delete,
408 keybinds::Key::Enter => Key::Enter,
409 keybinds::Key::Up => Key::Up,
410 keybinds::Key::Right => Key::Right,
411 keybinds::Key::Down => Key::Down,
412 keybinds::Key::Left => Key::Left,
413 keybinds::Key::Home => Key::Home,
414 keybinds::Key::End => Key::End,
415 keybinds::Key::PageUp => Key::PageUp,
416 keybinds::Key::PageDown => Key::PageDown,
417 keybinds::Key::Tab => Key::Tab,
418 _ => return None,
419 };
420
421 let mods = input.mods();
422 Some(Input {
423 key,
424 ctrl: mods.contains(Mods::CTRL),
425 alt: mods.contains(Mods::ALT),
426 shift: mods.contains(Mods::SHIFT),
427 })
428 }Trait Implementations§
Source§impl<'arbitrary> Arbitrary<'arbitrary> for KeyInput
impl<'arbitrary> Arbitrary<'arbitrary> for KeyInput
Source§fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
Generate an arbitrary value of
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
Generate an arbitrary value of
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§impl<'de> Deserialize<'de> for KeyInput
Available on crate feature serde only.
impl<'de> Deserialize<'de> for KeyInput
Available on crate feature
serde only.Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
Source§impl Display for KeyInput
impl Display for KeyInput
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Generate a string representation of the key input following the syntax.
use keybinds::{Key, Mods, KeyInput};
assert_eq!(format!("{}", KeyInput::new('x', Mods::CTRL)), "Ctrl+x");
assert_eq!(
format!("{}", KeyInput::new(Key::Enter, Mods::SHIFT | Mods::ALT)),
"Alt+Shift+Enter",
);Source§impl From<&Event> for KeyInput
Available on crate feature iced only.
impl From<&Event> for KeyInput
Available on crate feature
iced only.Source§fn from(event: &KeyEvent) -> Self
fn from(event: &KeyEvent) -> Self
Convert iced’s key events to KeyInput. Events except for key presses are converted into Key::Ignored with
no modifiers. Note that Shift modifier is removed when the pressed key is unnamed following the
syntax.
use keybinds::{KeyInput, Mods};
use iced::keyboard::{Event, Modifiers, Key};
// Key event for Ctrl+Shift+X
let event = Event::KeyPressed {
key: Key::Character("x".into()),
modified_key: Key::Character("X".into()),
modifiers: Modifiers::CTRL | Modifiers::SHIFT,
// ...
};
// `Mods::SHIFT` is removed because 'X' is already modified by Shift key
assert_eq!(KeyInput::from(event), KeyInput::new('X', Mods::CTRL));
// Events other than key presses are ignored
let event = Event::KeyReleased {
// ...
};
assert_eq!(KeyInput::from(event), KeyInput::from(keybinds::Key::Ignored));Source§impl From<&InputEvent> for KeyInput
Available on crate feature termwiz only.
impl From<&InputEvent> for KeyInput
Available on crate feature
termwiz only.Source§fn from(event: &InputEvent) -> Self
fn from(event: &InputEvent) -> Self
Converts to this type from the input type.
Source§impl From<InputEvent> for KeyInput
Available on crate feature termwiz only.
impl From<InputEvent> for KeyInput
Available on crate feature
termwiz only.Source§fn from(event: InputEvent) -> Self
fn from(event: InputEvent) -> Self
Converts to this type from the input type.
Source§impl FromStr for KeyInput
impl FromStr for KeyInput
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parse the key input from str following the syntax.
use keybinds::{Key, Mods, KeyInput};
assert_eq!("a".parse(), Ok(KeyInput::new('a', Mods::NONE)));
assert_eq!("Ctrl+x".parse(), Ok(KeyInput::new('x', Mods::CTRL)));
assert_eq!("Alt+Shift+Enter".parse(), Ok(KeyInput::new(Key::Enter, Mods::ALT | Mods::SHIFT)));
assert!("".parse::<KeyInput>().is_err());
assert!("Foooo".parse::<KeyInput>().is_err());
assert!("Shift+x".parse::<KeyInput>().is_err()); // Violates Shift modifier invariantimpl Copy for KeyInput
impl Eq for KeyInput
impl StructuralPartialEq for KeyInput
Auto Trait Implementations§
impl Freeze for KeyInput
impl RefUnwindSafe for KeyInput
impl Send for KeyInput
impl Sync for KeyInput
impl Unpin for KeyInput
impl UnwindSafe for KeyInput
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Convert the source color to the destination color using the specified
method.
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Convert the source color to the destination color using the bradford
method by default.
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Cast a collection of colors into a collection of arrays.
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Cast this collection of arrays into a collection of colors.
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
The number type that’s used in
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
Converts
self into C, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Cast a collection of colors into a collection of color components.
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
Performs a conversion from
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
Converts
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
Performs a conversion into
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
The number type that’s used in
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
Converts
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Convert into T with values clamped to the color defined bounds Read more
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Convert into T. The resulting color might be invalid in its color space Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
Converts
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
The error for when
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Try to cast this collection of color components into a collection of
colors. Read more
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
Convert into T, returning ok if the color is inside of its defined
range, otherwise an
OutOfBounds error is returned which contains
the unclamped color. Read moreSource§impl<C, U> UintsFrom<C> for Uwhere
C: IntoUints<U>,
impl<C, U> UintsFrom<C> for Uwhere
C: IntoUints<U>,
Source§fn uints_from(colors: C) -> U
fn uints_from(colors: C) -> U
Cast a collection of colors into a collection of unsigned integers.
Source§impl<C, U> UintsInto<C> for Uwhere
C: FromUints<U>,
impl<C, U> UintsInto<C> for Uwhere
C: FromUints<U>,
Source§fn uints_into(self) -> C
fn uints_into(self) -> C
Cast this collection of unsigned integers into a collection of colors.