#keyboard #terminal #tui #tty #termios

k_board

A keyboard event handler that allows dynamic development

9 stable releases

new 1.1.11 Nov 29, 2023
1.1.10 Nov 21, 2023
1.1.3 Oct 20, 2023

#80 in Text editors

Download history 123/week @ 2023-10-15 23/week @ 2023-10-22 69/week @ 2023-10-29 68/week @ 2023-11-05 23/week @ 2023-11-12 90/week @ 2023-11-19 67/week @ 2023-11-26

255 downloads per month
Used in nnrt

GPL-3.0 license

21KB
297 lines

k_board

crates.io github docs.rs

A lightweight keyboard mannager developed for dynamic programs by listening to keyboard events in raw mode (without the need to press enter). The handler has all the standard events of a western keyboard.

  • Gnu/Linux
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,                      @
    OpenExclamationMark,     ¡
    ClosedExclamationMark,   !
    QuotationMark,           "
    Backquote,               `
    AcuteAccent,             ´
    Home,
    End,
    Tab,
    Backtab,
    Insert,
    Letter(char),
    Number(u8),
    F(u8),
    Ctrl(char),
    Alt(char),
    Null,
}

Example

use k_board::{Keyboard, Keys};

fn main() {
    menu(0);
    for key in Keyboard::new() {
        match key {
            Keys::Up => menu(0),
            Keys::Down => menu(1),
            Keys::Enter => break,
            _ => {}
        }
    }
}

fn menu(operation: u8) {
    std::process::Command::new("clear").status().unwrap();
    let mut op: Vec<char> = vec!['*', ' '];
    if operation == 1 {
        op[0] = ' ';
        op[1] = '*';
    }
    println!(
        "[{}] I use k_board lightweight software\n[{}] I use heavyweight software",
        op[0], op[1]
    );
}

Contributing

Feel free to contribute to the repository. Make each modification to the code formatted with code before with cargo fmt. Below, a fragment that allows you to visualize in hexadecimal the key or the event executed on your keyboard:

use std::io::{Read, Write};

fn main() -> std::io::Result<()> {
    println!("Press a key or an keyboard event!");
    loop {
        let _ = get_key();
    }
}

pub fn get_key() -> std::io::Result<()> {
    let termios_enviroment: k_board::termios = k_board::setup_raw_mode().unwrap();
    std::io::stdout().flush().unwrap();
    let mut buffer: [u8; 3] = [0; 3];
    std::io::stdin().read(&mut buffer).unwrap();
    if buffer[0] != 0x00 {
        println!(
            "[0x{:x?}, 0x{:x?}, 0x{:x?}]",
            buffer[0], buffer[1], buffer[2]
        );
    }
    std::io::stdout().flush().unwrap();
    k_board::restore_termios(&termios_enviroment).unwrap();
    Ok(())
}

No runtime deps