Skip to content

Commit

Permalink
Use as_key_press() in example
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Nov 26, 2024
1 parent 6323263 commit 5dea800
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
67 changes: 33 additions & 34 deletions examples/event-match-modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

_ => {}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ impl Event {
/// while !event::read()?.is_key_press() {
/// // ...
/// }
/// # Ok::<(), std::io::Error>(())
/// ```
#[inline]
pub fn is_key_press(&self) -> bool {
Expand Down

0 comments on commit 5dea800

Please sign in to comment.