- KeyEventKind
- KeyModifiers
- CONTROL
- SHIFT
- ESC
Ratatui - handle keyboard events
- We move the text to be displayed to a variable so we can change it.
- We accept every key on the keyboard.
- Pressing ESCape or Ctrl-C will terminate the application.
- Any other key or key-combination will be displayed on the screen. (e.g. Left, Right)
examples/ratatui/handle-the-keyboard/Cargo.toml
[package] name = "hello-world" version = "0.1.0" edition = "2021" [dependencies] crossterm = "0.28.1" ratatui = "0.29.0"
examples/ratatui/handle-the-keyboard/src/main.rs
use std::io; use crossterm::event::KeyModifiers; use ratatui::{ crossterm::event::{self, KeyCode, KeyEventKind}, style::Stylize, widgets::Paragraph, DefaultTerminal, }; fn main() -> io::Result<()> { let mut terminal = ratatui::init(); terminal.clear()?; let app_result = run(terminal); ratatui::restore(); app_result } fn run(mut terminal: DefaultTerminal) -> io::Result<()> { let mut text = String::new(); loop { terminal.draw(|frame| { let greeting = Paragraph::new(text.clone()).white().on_blue(); frame.render_widget(greeting, frame.area()); })?; if let event::Event::Key(key) = event::read()? { if key.kind == KeyEventKind::Press { // if key.code == KeyCode::Char('q') { // return Ok(()); // } //text.push_str("a"); if key.code == KeyCode::Esc { return Ok(()); } if key.modifiers == KeyModifiers::CONTROL && key.code == KeyCode::Char('c') { return Ok(()); } // // println!("{}", key.modifiers); // if key.modifiers == KeyModifiers::SHIFT { // text.push_str("Shift+"); // text.push_str(&key.code.to_string()); // } else { // text.push_str(&key.code.to_string()); // } //text.push_str(&key.code.to_string()); text = format!("{} - {}", key.modifiers, key.code); } } } }