DEV Community

Stacy Roll
Stacy Roll

Posted on

How to get the console in raw mode in rust ๐Ÿ–ค

Rust by default does not have a console management in pure raw mode. If you are looking for a way to handle keyboard events you can add the lightweight k_board library. Here is an example and the structure offered in the official documentation.

cargo add k_board

use k_board::{Keyboard, Keys};

fn main() {
    for key in Keyboard::new() {
        match key {
            Keys::Up => //code 1
            Keys::Down => //code 2
            Keys::Enter => break,
            _ => {}
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
pub enum Keys {
    Up,
    Down,
    Left,
    Right,
    Enter,
    Space,
    Delete,
    Escape,
    Plus,
    Minus,
    Equal,
    Power,                 ^
    Slash,                 /
    Backslash,             \
    Asterisk,              *
    Point,
    Comma,
    Hashtag,
    Pipe,                  |
    Percent,               %
    Ampersand,
    Currency,              $
    TwoPoints,             :
    Semicolon,             ;
    OpenSquareBracket,     [
    CloseSquareBracket,    ]
    OpenCurlyBrace,        {
    CloseCurlyBrace,       }
    OpenQuestionMark,      ยฟ
    CloseQuestionMark,     ?
    OpenParenthesis,       (
    CloseParenthesis,      )
    LessThan,              <
    GreaterThan,           >
    Apostrophe,            ' 
    At,                    @
    Home,
    End,
    Tab,
    Backtab,
    Insert,
    Letter(char),
    Number(u8),
    F(u8),
    Ctrl(char),
    Alt(char),
    Null,
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)