Monday, December 18, 2023

Rust - Read a Single Keypress from the Keyboard, ala 'getch'

Rust - Read a Single Keypress from the Keyboard, ala 'getch'

How to Read a Keypress from the Keyboard in Rust, similar to getch()

Kent West - kent.west@{that mail that swore to do no evil}

I struggled for days to figure out how to get a simple keypress from the keyboard. Finally! I found a crate on crates.io that makes it as simple as using "getch()" from the ncurses library.

The reason I did not use ncurses is because it requires an initialization step, which swaps out the screen for a new blank screen, which then disappears when you're finished with the curses mode.

I would have used the termion crate, but search high and low on the 'Net for a simple snippet of code that shows just the absolute bare minimum for getting a keypress, and you'll get a headache, and no solution, unless you're smarter than I am (granted, that's not a very high bar, but still...) I do not understand why coders will not give the simplest, barest-possible, COMMENTED!!!! code snippets so the newbie can get started. (Yes, I'm angry about this; Rust could be such a beautiful platform, if non-life-long-coders could learn how to code in it from simple examples and good explanations.)

Here's how to get a keypress from the keyboard in Rust:

Configure 'Cargo.toml'
$ cargo add getchar
src/main.rs
fn main() {
    let keypress = getchar::getchar().unwrap(); // getchar() returns an "Option<char>" type that must be unwrapped to get to the 'char' goody inside.

    println!("You pressed {}", keypress);
} // end of main()

No comments: