Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Display for KeyCode and KeyModifiers #862

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/key-display.rs
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(())
}
Loading