A counter that listen keyboard events 🕹️

a-counter-that-listen-keyboard-events-️

How to make a dynamic number counter so that when you press the up arrow, a number is added and when you press the down arrow, a number is subtracted?

I asked myself that question and proceeded to try to solve that algorithm in my mind first and then in code, here is the result.

Let’s add the keyboard handler k_board

cargo add k_board

Let’s add the program logic:

use k_board::{Keyboard, Keys};

fn main() {
    let mut number: i8 = 0;
    print_number(&mut number, 0);
    for key in Keyboard::new() {
        match key {
            Keys::Up => print_number(&mut number, 1),
            Keys::Down => print_number(&mut number, -1),
            Keys::Enter => break,
            _ => {}
        }
    }
}

fn print_number(number: &mut i8, operation: i8) {
    std::process::Command::new("clear").status().unwrap();
    *number += operation;
    println!("{}", number);
}

See you soon love ❤️

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
incremental-benefits-of-asq-membership

Incremental Benefits of ASQ Membership

Next Post
9-questions-to-ask-yourself-before-designing-a-brand-logo

9 Questions To Ask Yourself Before Designing a Brand Logo

Related Posts