minicrossterm/crossterm_input/examples/key_events.rs
Timon 1e332daaed
Refactor and API stabilization (#115)
- Major refactor and cleanup.
- Improved performance; 
    - No locking when writing to stdout. 
    - UNIX doesn't have any dynamic dispatch anymore. 
    - Windows has improved the way to check if ANSI modes are enabled.
    - Removed lot's of complex API calls: `from_screen`, `from_output`
    - Removed `Arc<TerminalOutput>` from all internal Api's. 
- Removed termios dependency for UNIX systems.
- Upgraded deps.
- Removed about 1000 lines of code
    - `TerminalOutput` 
    - `Screen`
    - unsafe code
    - Some duplicated code introduced by a previous refactor.
- Raw modes UNIX systems improved     
- Added `NoItalic` attribute
2019-04-10 23:46:30 +02:00

135 lines
4.4 KiB
Rust

extern crate crossterm_input;
extern crate crossterm_screen;
use crossterm_input::{InputEvent, KeyEvent, MouseButton, MouseEvent, TerminalInput};
use crossterm_screen::Screen;
use std::{thread, time::Duration};
fn process_input_event(key_event: InputEvent) -> bool {
match key_event {
InputEvent::Keyboard(k) => {
match k {
KeyEvent::Char(c) => match c {
'q' => {
println!("The 'q' key is hit and the program is not listening to input anymore.\n\n");
return true;
}
_ => {
println!("{}", format!("'{}' pressed\n\n", c));
}
},
KeyEvent::Alt(c) => {
println!("{}", format!("ALT +'{}' pressed\n\n", c));
}
KeyEvent::Ctrl(c) => {
println!("{}", format!("CTRL +'{}' Pressed\n\n", c));
}
KeyEvent::Esc => {
println!("{}", format!("ESC pressed\n\n"));
}
KeyEvent::F(number) => {
println!("{}", format!("F{} key pressed\n\n", number));
}
KeyEvent::PageUp => {
println!("{}", format!("Page Up\n\n"));
}
KeyEvent::PageDown => {
println!("{}", format!("Page Down\n\n"));
}
KeyEvent::Delete => {
println!("{}", format!("Delete\n\n"));
}
_ => {
println!("{}", format!("OTHER: {:?}\n\n", k));
()
}
}
}
InputEvent::Mouse(m) => match m {
MouseEvent::Press(b, x, y) => match b {
MouseButton::Left => {
println!("{}", format!("left mouse press @ {}, {}\n\n", x, y));
}
MouseButton::Right => {
println!("{}", format!("right mouse press @ {}, {}\n\n", x, y));
}
MouseButton::Middle => {
println!("{}", format!("mid mouse press @ {}, {}\n\n", x, y));
}
MouseButton::WheelUp => println!("{}", format!("wheel up @ {}, {}\n\n", x, y)),
MouseButton::WheelDown => {
println!("{}", format!("wheel down @ {}, {}\n\n", x, y));
}
},
MouseEvent::Release(x, y) => {
println!("{}", format!("mouse released @ {}, {}\n\n", x, y));
}
MouseEvent::Hold(x, y) => {
println!("{}", format!("dragging @ {}, {}\n\n", x, y));
}
_ => {
println!("{}", "Unknown mouse event");
}
},
_ => println!("Unknown!"),
}
return false;
}
pub fn read_asynchronously() {
// make sure to enable raw mode, this will make sure key events won't be handled by the terminal it's self and allows crossterm to read the input and pass it back to you.
let _ = Screen::new(true);
let input = input();
// enable mouse events to be captured.
input.enable_mouse_mode().unwrap();
let mut stdin = input.read_async();
loop {
if let Some(key_event) = stdin.next() {
if process_input_event(key_event) {
break;
}
}
thread::sleep(Duration::from_millis(50));
}
// disable mouse events to be captured.
input.disable_mouse_mode().unwrap();
} // <=== background reader will be disposed when dropped.
pub fn read_synchronously() {
// make sure to enable raw mode, this will make sure key events won't be handled by the terminal it's self and allows crossterm to read the input and pass it back to you.
let _ = Screen::new(true);
let input = input();
// enable mouse events to be captured.
input.enable_mouse_mode().unwrap();
let mut sync_stdin = input.read_sync();
loop {
let event = sync_stdin.next();
if let Some(key_event) = event {
if process_input_event(key_event) {
break;
}
}
}
// disable mouse events to be captured.
input.disable_mouse_mode().unwrap();
}
fn main() {
// un-comment below and run with
// `cargo run --example key_events`:
// read_synchronously();
// read_asynchronously();
}