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 480b436
Showing 1 changed file with 33 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

0 comments on commit 480b436

Please sign in to comment.