| 1 |
//! Demonstrates how to block read events. |
| 2 |
//! |
| 3 |
//! cargo run --example event-read |
| 4 |
|
| 5 |
use std::io; |
| 6 |
|
| 7 |
use crossterm::event::{ |
| 8 |
poll, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags, |
| 9 |
}; |
| 10 |
use crossterm::{ |
| 11 |
cursor::position, |
| 12 |
event::{ |
| 13 |
read, DisableBracketedPaste, DisableFocusChange, DisableMouseCapture, EnableBracketedPaste, |
| 14 |
EnableFocusChange, EnableMouseCapture, Event, KeyCode, |
| 15 |
}, |
| 16 |
execute, queue, |
| 17 |
terminal::{disable_raw_mode, enable_raw_mode}, |
| 18 |
}; |
| 19 |
use std::time::Duration; |
| 20 |
|
| 21 |
const HELP: &str = r#"Blocking read() |
| 22 |
- Keyboard, mouse, focus and terminal resize events enabled |
| 23 |
- Hit "c" to print current cursor position |
| 24 |
- Use Esc to quit |
| 25 |
"#; |
| 26 |
|
| 27 |
fn print_events() -> io::Result<()> { |
| 28 |
loop { |
| 29 |
// Blocking read |
| 30 |
let event = read()?; |
| 31 |
|
| 32 |
println!("Event: {:?}\r", event); |
| 33 |
|
| 34 |
if event == Event::Key(KeyCode::Char('c').into()) { |
| 35 |
println!("Cursor position: {:?}\r", position()); |
| 36 |
} |
| 37 |
|
| 38 |
if let Event::Resize(x, y) = event { |
| 39 |
let (original_size, new_size) = flush_resize_events((x, y)); |
| 40 |
println!("Resize from: {:?}, to: {:?}\r", original_size, new_size); |
| 41 |
} |
| 42 |
|
| 43 |
if event == Event::Key(KeyCode::Esc.into()) { |
| 44 |
break; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
Ok(()) |
| 49 |
} |
| 50 |
|
| 51 |
// Resize events can occur in batches. |
| 52 |
// With a simple loop they can be flushed. |
| 53 |
// This function will keep the first and last resize event. |
| 54 |
fn flush_resize_events(first_resize: (u16, u16)) -> ((u16, u16), (u16, u16)) { |
| 55 |
let mut last_resize = first_resize; |
| 56 |
while let Ok(true) = poll(Duration::from_millis(50)) { |
| 57 |
if let Ok(Event::Resize(x, y)) = read() { |
| 58 |
last_resize = (x, y); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
(first_resize, last_resize) |
| 63 |
} |
| 64 |
|
| 65 |
fn main() -> io::Result<()> { |
| 66 |
println!("{}", HELP); |
| 67 |
|
| 68 |
enable_raw_mode()?; |
| 69 |
|
| 70 |
let mut stdout = io::stdout(); |
| 71 |
|
| 72 |
let supports_keyboard_enhancement = matches!( |
| 73 |
crossterm::terminal::supports_keyboard_enhancement(), |
| 74 |
Ok(true) |
| 75 |
); |
| 76 |
|
| 77 |
if supports_keyboard_enhancement { |
| 78 |
queue!( |
| 79 |
stdout, |
| 80 |
PushKeyboardEnhancementFlags( |
| 81 |
KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES |
| 82 |
| KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES |
| 83 |
| KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS |
| 84 |
| KeyboardEnhancementFlags::REPORT_EVENT_TYPES |
| 85 |
) |
| 86 |
)?; |
| 87 |
} |
| 88 |
|
| 89 |
execute!( |
| 90 |
stdout, |
| 91 |
EnableBracketedPaste, |
| 92 |
EnableFocusChange, |
| 93 |
EnableMouseCapture, |
| 94 |
)?; |
| 95 |
|
| 96 |
if let Err(e) = print_events() { |
| 97 |
println!("Error: {:?}\r", e); |
| 98 |
} |
| 99 |
|
| 100 |
if supports_keyboard_enhancement { |
| 101 |
queue!(stdout, PopKeyboardEnhancementFlags)?; |
| 102 |
} |
| 103 |
|
| 104 |
execute!( |
| 105 |
stdout, |
| 106 |
DisableBracketedPaste, |
| 107 |
DisableFocusChange, |
| 108 |
DisableMouseCapture |
| 109 |
)?; |
| 110 |
|
| 111 |
disable_raw_mode() |
| 112 |
} |