0

I am using crossterm to get user input. I'm trying to parse any character press. In the match block here, I am matching specific code/modifier combos, but I would like to match any char. I'm not sure what the syntax is.

//importing in execute! macro
#[macro_use]
extern crate crossterm;

use crossterm::cursor;
use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::style::Print;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType};
use std::io::{stdout, Write};

fn main() {
    let mut stdout = stdout();
    enable_raw_mode().unwrap();

    execute!(stdout, Clear(ClearType::All), cursor::MoveTo(0, 0), Print(r#"ctrl + q to exit, ctrl + h to print "Hello world", alt + t to print "crossterm is cool""#))
            .unwrap();

    loop {
        execute!(stdout, cursor::MoveTo(0, 0)).unwrap();
        match read().unwrap() {
            Event::Key(KeyEvent {
                code: KeyCode::Char('h'),
                modifiers: KeyModifiers::CONTROL,
            }) => execute!(stdout, Clear(ClearType::All), Print("Hello world!")).unwrap(),
            Event::Key(KeyEvent {
                code: KeyCode::Char('q'),
                modifiers: KeyModifiers::CONTROL,
            }) => break,
            _ => (),
        }
    }

    //disabling raw mode
    disable_raw_mode().unwrap();
}
Peter Kapteyn
  • 354
  • 1
  • 13
  • 1
    Just match on `Event::Key (evt)` if you want to do something more depending on the key pressed, or just `Event::Key (_)` to match on any key and discard the details. – Jmb Aug 19 '21 at 06:40
  • 1
    See also [this section](https://doc.rust-lang.org/stable/book/ch18-03-pattern-syntax.html#destructuring-to-break-apart-values) in the Rust book. – Jmb Aug 19 '21 at 06:42

0 Answers0