-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Display for KeyCode and KeyModifiers (#862)
Partially addresses #792 * Implement Display for KeyCode and KeyModifiers * Add demo for Display implementation
- Loading branch information
Showing
2 changed files
with
354 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! Demonstrates the display format of key events. | ||
//! | ||
//! This example demonstrates the display format of key events, which is useful for displaying in | ||
//! the help section of a terminal application. | ||
//! | ||
//! cargo run --example key-display | ||
use std::io; | ||
|
||
use crossterm::event::{KeyEventKind, KeyModifiers}; | ||
use crossterm::{ | ||
event::{read, Event, KeyCode}, | ||
terminal::{disable_raw_mode, enable_raw_mode}, | ||
}; | ||
|
||
const HELP: &str = r#"Key display | ||
- Press any key to see its display format | ||
- Use Esc to quit | ||
"#; | ||
|
||
fn main() -> io::Result<()> { | ||
println!("{}", HELP); | ||
enable_raw_mode()?; | ||
if let Err(e) = print_events() { | ||
println!("Error: {:?}\r", e); | ||
} | ||
disable_raw_mode()?; | ||
Ok(()) | ||
} | ||
|
||
fn print_events() -> io::Result<()> { | ||
loop { | ||
let event = read()?; | ||
match event { | ||
Event::Key(event) if event.kind == KeyEventKind::Press => { | ||
print!("Key pressed: "); | ||
if event.modifiers != KeyModifiers::NONE { | ||
print!("{}+", event.modifiers); | ||
} | ||
println!("{}\r", event.code); | ||
if event.code == KeyCode::Esc { | ||
break; | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
Ok(()) | ||
} |
Oops, something went wrong.