From 5dea800b6890a54c4af0e33ecc6f2a3f63170045 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Mon, 25 Nov 2024 18:29:03 -0800 Subject: [PATCH] Use as_key_press() in example --- examples/event-match-modifiers.rs | 67 +++++++++++++++---------------- src/event.rs | 1 + 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/examples/event-match-modifiers.rs b/examples/event-match-modifiers.rs index c3f75e95d..ac9daefda 100644 --- a/examples/event-match-modifiers.rs +++ b/examples/event-match-modifiers.rs @@ -4,43 +4,42 @@ use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; -fn match_event(read_event: Event) { - match read_event { - // Match one one modifier: - Event::Key(KeyEvent { - modifiers: KeyModifiers::CONTROL, - code, - .. - }) => { - println!("Control + {:?}", code); - } - Event::Key(KeyEvent { - modifiers: KeyModifiers::SHIFT, - code, - .. - }) => { - println!("Shift + {:?}", code); - } - Event::Key(KeyEvent { - modifiers: KeyModifiers::ALT, - code, - .. - }) => { - println!("Alt + {:?}", code); - } +fn match_event(event: Event) { + if let Some(key) = event.as_key_press() { + match key { + KeyEvent { + modifiers: KeyModifiers::CONTROL, + code, + .. + } => { + println!("Control + {:?}", code); + } + KeyEvent { + modifiers: KeyModifiers::SHIFT, + code, + .. + } => { + println!("Shift + {:?}", code); + } + KeyEvent { + modifiers: KeyModifiers::ALT, + code, + .. + } => { + println!("Alt + {:?}", code); + } - // Match on multiple modifiers: - Event::Key(KeyEvent { - code, modifiers, .. - }) => { - if modifiers == (KeyModifiers::ALT | KeyModifiers::SHIFT) { - println!("Alt + Shift {:?}", code); - } else { - println!("({:?}) with key: {:?}", modifiers, code) + // Match on multiple modifiers: + KeyEvent { + code, modifiers, .. + } => { + if *modifiers == (KeyModifiers::ALT | KeyModifiers::SHIFT) { + println!("Alt + Shift {:?}", code); + } else { + println!("({:?}) with key: {:?}", modifiers, code) + } } } - - _ => {} } } diff --git a/src/event.rs b/src/event.rs index ece2658bb..c36f740eb 100644 --- a/src/event.rs +++ b/src/event.rs @@ -580,6 +580,7 @@ impl Event { /// while !event::read()?.is_key_press() { /// // ... /// } + /// # Ok::<(), std::io::Error>(()) /// ``` #[inline] pub fn is_key_press(&self) -> bool {