From 121620e64f63a1cc9ada8b2aca1714805a2bfdde Mon Sep 17 00:00:00 2001 From: Olivier FAURE Date: Tue, 30 Sep 2025 13:36:26 +0200 Subject: [PATCH] Add `KeyboardEvent::key_down` and `KeyboardEvent::key_up` constructors. Closes #80. --- src/keyboard_event.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/keyboard_event.rs b/src/keyboard_event.rs index 01946af..a3c7f85 100644 --- a/src/keyboard_event.rs +++ b/src/keyboard_event.rs @@ -23,3 +23,27 @@ pub struct KeyboardEvent { /// and instead [composition events](crate::CompositionEvent) should be used. pub is_composing: bool, } + +impl KeyboardEvent { + /// Convenience constructor which takes `key` and `code`, sets `state` to + /// [`KeyState::Down`], and sets everything else to default values. + pub fn key_down(key: impl Into, code: Code) -> Self { + KeyboardEvent { + state: KeyState::Down, + key: key.into(), + code, + ..Default::default() + } + } + + /// Convenience constructor which takes `key` and `code`, sets `state` to + /// [`KeyState::Up`], and sets everything else to default values. + pub fn key_up(key: impl Into, code: Code) -> Self { + KeyboardEvent { + state: KeyState::Up, + key: key.into(), + code, + ..Default::default() + } + } +}