pub struct KeyState { /* private fields */ }Expand description
Tracks which keys are currently held down.
Feed it every KeyEvent (or Event) you receive and query
is_held each frame for held-key movement. A key is
considered held from its first KeyEventKind::Press until a matching
KeyEventKind::Release, or until apply_event sees an
Event::FocusLost, whichever comes first.
Held keys are keyed by (KeyCode, KeyLocation), so a held Numpad8 and a held digit-row 8 are
tracked separately: is_held takes the pair, and held yields
it.
Release events are what actually clear a key, and not every backend emits them: plain
terminals only ever report KeyEventKind::Press (see KeyEventKind). On those
press-only backends a key never leaves the held set on its own – not even on focus loss, since
there is no release to match – so call clear at a suitable boundary (e.g.
once per turn) if you rely on held-key state there. Backends rich enough to emit releases
(winit, or a terminal with the kitty keyboard protocol) are exactly the ones that also emit
Event::FocusLost, so apply_event clears the
held set on it: without that, alt-tabbing or clicking away while a key is down would leave it
stuck held forever, since the release is delivered to whichever window/app gains focus instead.
Implementations§
Source§impl KeyState
impl KeyState
Sourcepub fn apply_event(&mut self, event: &Event)
pub fn apply_event(&mut self, event: &Event)
Updates the held set from an Event.
Key events update the held set as apply does;
Event::FocusLost clears it entirely (see the type-level docs
for why); every other event is ignored.
Sourcepub fn is_held(&self, code: KeyCode, location: KeyLocation) -> bool
pub fn is_held(&self, code: KeyCode, location: KeyLocation) -> bool
Returns true if code at location is currently held.
Sourcepub fn held(&self) -> impl Iterator<Item = (KeyCode, KeyLocation)>
pub fn held(&self) -> impl Iterator<Item = (KeyCode, KeyLocation)>
Iterates the currently held (code, location) pairs, in first-pressed order.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for KeyState
impl<'de> Deserialize<'de> for KeyState
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<KeyState, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<KeyState, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for KeyState
impl Serialize for KeyState
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for KeyState
impl RefUnwindSafe for KeyState
impl Send for KeyState
impl Sync for KeyState
impl Unpin for KeyState
impl UnsafeUnpin for KeyState
impl UnwindSafe for KeyState
Blanket Implementations§
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.