DEV Community

Stacy Roll
Stacy Roll

Posted on • Edited on

How to get the F1, F2, F3 ... keys in Rust ? ๐Ÿ’ฌ

If you're using a GNU/Linux operating system and want to develop a system that requires the F1, F2, F3, and so on keys as inputs, I recommend reading this short article on how to do it!

First of all, let's add the library that will allow us to interact with keyboard events at a low level. I'm talking about k_board.

cargo add k_board

Let's add the program's logic:

use k_board::{Keyboard, Keys};

fn main() {
    for key in Keyboard::new() {
        match key {
            Keys::F(1) => /*run some function*/,
            Keys::F(5) => /*update screen*/,
            Keys::F(11) => /*full screen*/,
            Keys::Enter => break,
            _ => {}
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

k_board has matched from F1 to F12, here is the source code.

Top comments (0)