How to Check if Right & Left Ctrl/Strg is pressed?

Hello,
how to check if Right or Left Ctrl/Strg is pressed ? ( without pressed any other additional key )

main.rs

use k_board::{keyboard::{get_key_from_keyboard, Keyboard}, keys::Keys};

fn main() {
    for key in Keyboard::new() {
        match key {
            Keys::Char('q') => break,
            Keys::CtrlL() => println!("Ctrl left"),
            Keys::CtrlR() => println!("Ctrl right"),
            _ => (),
        }
    }
}

mod of keys.rs

// All special character keys
#[cfg(any(feature = "standar", feature = "full"))]
pub const STANDAR: [([u8; BYTES], Keys); 40] = [
    ([0xA2, 0x00, 0x00], Keys::CtrlR),
    ([0xA3, 0x00, 0x00], Keys::CtrlL),

...

$ cargo check

    Checking key v0.1.0 (/home/developer/rust/key)
warning: unused import: `get_key_from_keyboard`
 --> src/main.rs:1:26
  |
1 | use k_board::{keyboard::{get_key_from_keyboard, Keyboard}, keys::Keys};
  |                          ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0599]: no variant or associated item named `CtrlL` found for enum `k_board::keys::Keys` in the current scope
 --> src/main.rs:8:19
  |
8 |             Keys::CtrlL() => println!("Ctrl left"),
  |                   ^^^^^ variant or associated item not found in `Keys`
  |
help: there is a variant with a similar name
  |
8 |             Keys::Ctrl() => println!("Ctrl left"),
  |                   ~~~~

error[E0599]: no variant or associated item named `CtrlR` found for enum `k_board::keys::Keys` in the current scope
 --> src/main.rs:9:19
  |
9 |             Keys::CtrlR() => println!("Ctrl right"),
  |                   ^^^^^ variant or associated item not found in `Keys`
  |
help: there is a variant with a similar name
  |
9 |             Keys::Ctrl() => println!("Ctrl right"),
  |                   ~~~~

For more information about this error, try `rustc --explain E0599`.
warning: `key` (bin "key") generated 1 warning
error: could not compile `key` (bin "key") due to 2 previous errors; 1 warning emitted

The k_board crate has a number of features that must be enabled to use many of the APIs. You enable features in your Cargo.toml when you define the dependency:
https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies

@jumpnbrownweasel

thanks use full - but ...

cargo add k_board -F full


[dependencies]
k_board = { version = "1.3.1", features = ["full"] }

  --> /home/developer/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/
k_board-1.3.1/src/keys.rs:61:26
   |
41 |     CtrlR(),
   |     ----- `CtrlR` defines an enum variant constructor here, which should be called
...
61 |     ([0xA2, 0x00, 0x00], Keys::CtrlR),
   |                          ^^^^^^^^^^^ expected `Keys`, found enum constructor
   |
   = note:          expected enum `keys::Keys`
           found enum constructor `fn() -> keys::Keys {keys::Keys::CtrlR}`
help: use parentheses to construct this tuple variant
   |
61 |     ([0xA2, 0x00, 0x00], Keys::CtrlR()),
   |                                     ++

error[E0308]: mismatched types
  --> /home/developer/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/k_board-1.3.1/src/keys.rs:62:26
   |
42 |     CtrlL(),
   |     ----- `CtrlL` defines an enum variant constructor here, which should be called
...
62 |     ([0xA3, 0x00, 0x00], Keys::CtrlL),
   |                          ^^^^^^^^^^^ expected `Keys`, found enum constructor
   |
   = note:          expected enum `keys::Keys`
           found enum constructor `fn() -> keys::Keys {keys::Keys::CtrlL}`
help: use parentheses to construct this tuple variant
   |
62 |     ([0xA3, 0x00, 0x00], Keys::CtrlL()),
   |                                     ++

error[E0308]: mismatched types
   --> /home/developer/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/k_board-1.3.1/src/keys.rs:60:48
    |
60  |   pub const STANDAR: [([u8; BYTES], Keys); 40] = [
    |  __________________________________________--____^
    | |                                          |
    | |                                          help: consider specifying the actual array length: `42`
61  | |     ([0xA2, 0x00, 0x00], Keys::CtrlR),
62  | |     ([0xA3, 0x00, 0x00], Keys::CtrlL),
63  | |     ([0x1B, 0x5B, 0x48], Keys::Home),
...   |
102 | |     ([0xc2, 0xb4, 0x00], Keys::Char('´')),
103 | | ];
    | |_^ expected an array with a size of 40, found one with a size of 42
    |
    = note: expected array `[([u8; 3], keys::Keys); 40]`
               found array `[([u8; 3], keys::Keys); 42]`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `k_board` (lib) due to 3 previous errors

The k_board version 1.3.1 (or any version as far as I can see) does not appear to have any CtrlL or CtrlR constants or enum variants. Where did you get those? Are they your own change on top of 1.3.1? If so, you want to change CtrlL() to CtrlL and CtrlR() to CtrlR in the Keys enum, and then update the size of the STANDAR array as suggested by the compiler.

Are they your own change

yes my try to add this keys

Did I understand right you mean:

[dependencies]
k_board = { version = "1.3.1", features = ["standar"] }



pub enum Keys {
    Up,
    Down,
    Left,
    Right,
    Enter,
    Home,
    Tab,
    Backtab,
    Space,
    Delete,
    Escape,
    End,
    Char(char),
    F(u8),
    CtrlR,
    CtrlL,
    Ctrl(char),
    Alt(char),
    AltGr(char),
    Null,
}

$ cargo check

    Checking k_board v1.3.1
error[E0308]: mismatched types
   --> /home/developer/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/k_board-1.3.1/src/keys.rs:60:48
    |
60  |   pub const STANDAR: [([u8; BYTES], Keys); 40] = [
    |  __________________________________________--____^
    | |                                          |
    | |                                          help: consider specifying the actual array length: `42`
61  | |     ([0xA2, 0x00, 0x00], Keys::CtrlR),
62  | |     ([0xA3, 0x00, 0x00], Keys::CtrlL),
63  | |     ([0x1B, 0x5B, 0x48], Keys::Home),
...   |
102 | |     ([0xc2, 0xb4, 0x00], Keys::Char('´')),
103 | | ];
    | |_^ expected an array with a size of 40, found one with a size of 42
    |
    = note: expected array `[([u8; 3], keys::Keys); 40]`
               found array `[([u8; 3], keys::Keys); 42]`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `k_board` (lib) due to 1 previous error

main.rs

use k_board::{keyboard::Keyboard, keys::Keys};

fn main() {
    for key in Keyboard::new() {
        match key {
            Keys::Char('q') => break,
            Keys::CtrlL => println!("Ctrl left"),
            Keys::CtrlR => println!("Ctrl right"),
            _ => (),
        }
    }
}

keys.rs


/// Keys enum
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Keys {
    Up,
    Down,
    Left,
    Right,
    Enter,
    Home,
    Tab,
    Backtab,
    Space,
    Delete,
    Escape,
    End,
    Char(char),
    F(u8),
    CtrlR,
    CtrlL,
    Ctrl(char),
    Alt(char),
    AltGr(char),
    Null,
}

...

/// All special character keys
#[cfg(any(feature = "standar", feature = "full"))]
pub const STANDAR: [([u8; BYTES], Keys); 42] = [
    ([0xA2, 0x00, 0x00], Keys::CtrlR),
    ([0xA3, 0x00, 0x00], Keys::CtrlL),

...
...

many thanks!

now it builds the binary - but the keypress of ctrl right or legt is not printed

and q for quit also no work

As far as I know, to detect that a modifier key has been pressed by itself in a terminal program requires a terminal that supports the Kitty keyboard protocol’s “report all keys as escape codes” functionality, and a crate that also supports that functionality, like crossterm. k_board does not appear to be able to do this.

The code to check for control keys in this case looks like this:

use std::io;
use std::process::exit;

use crossterm::{
    event::{
        self, Event, KeyCode, KeyEvent, KeyEventKind, KeyboardEnhancementFlags, ModifierKeyCode,
        PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags,
    },
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, supports_keyboard_enhancement},
};

fn main() -> io::Result<()> {
    if !supports_keyboard_enhancement()? {
        eprintln!("Keyboard enhancement not supported.");
        exit(1);
    }

    enable_raw_mode()?;
    execute!(
        io::stdout(),
        PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES)
    )?;

    loop {
        match event::read()? {
            Event::Key(KeyEvent {
                code: KeyCode::Char('q'),
                ..
            }) => break,
            Event::Key(KeyEvent {
                code: KeyCode::Modifier(ModifierKeyCode::LeftControl),
                kind: KeyEventKind::Press,
                ..
            }) => print!("Ctrl left\r\n"),
            Event::Key(KeyEvent {
                code: KeyCode::Modifier(ModifierKeyCode::RightControl),
                kind: KeyEventKind::Press,
                ..
            }) => print!("Ctrl right\r\n"),
            _ => (),
        }
    }

    execute!(io::stdout(), PopKeyboardEnhancementFlags)?;
    disable_raw_mode()
}

Thanks a lot

Solution: sudo apt-get install kitty
kitty
cd path/to/project
target/release/myproject

looks like the xfce-terminal or xterm did not support this
sad is that the alacritty also not work... but the website say is supported

got this messages from the compile programm: " Keyboard enhancement not supported. "

Cargo.toml

[dependencies]
crossterm = { version = "0.28.1", features = ["events"] }

Support was only added to alacritty in 0.13.0. This might be newer than you have preinstalled; for example, on Debian I only have 0.11.0 and the enhanced keyboard protocol is not available. After installing 0.15.1 with Cargo, it works in Alacritty as well.