Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not capture MediaKeyCodes #897

Open
ZeroByter opened this issue Jun 4, 2024 · 0 comments
Open

Can not capture MediaKeyCodes #897

ZeroByter opened this issue Jun 4, 2024 · 0 comments

Comments

@ZeroByter
Copy link

Describe the bug
When listening to keyboard events, we should be able to also capture media key code events, such as for example MediaKeyCode::Stop, but this doesn't work.

To Reproduce
Run this code:

use std::io::stdout;
use std::time::Duration;

use crossterm::event::{
    self, Event, KeyCode, KeyEvent, KeyboardEnhancementFlags, MediaKeyCode,
    PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags,
};
use crossterm::execute;
use crossterm::terminal::{
    disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};

fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    let mut stdout = stdout();

    execute!(stdout, EnterAlternateScreen)?;

    enable_raw_mode();

    let push_result = execute!(
        stdout,
        PushKeyboardEnhancementFlags(
            KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
                | KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
        )
    );

    println!("PushKeyboardEnhancementFlags: {:?}", push_result);

    loop {
        // Wait for an event with a timeout of 500 milliseconds
        if crossterm::event::poll(Duration::from_millis(500))? {
            // Read the event
            if let Event::Key(KeyEvent {
                code,
                modifiers,
                kind,
                state,
            }) = event::read()?
            {
                println!("modifiers: {:?}", modifiers);
                println!("kind: {:?}", kind);
                println!("state: {:?}", state);

                match code {
                    KeyCode::Media(code) => match code {
                        MediaKeyCode::Stop => {
                            println!("Stop!")
                        }
                        _ => println!("Other media key pressed: {:?}", code),
                    },
                    // Handle other KeyCode variants here
                    KeyCode::Char('q') => {
                        println!("Exiting...");
                        break;
                    }
                    _ => println!("Other key pressed: {:?}", code),
                }
            }
        }
    }

    let pop_result = execute!(stdout, PopKeyboardEnhancementFlags);

    println!("PushKeyboardEnhancementFlags: {:?}", pop_result);

    execute!(stdout, LeaveAlternateScreen)?;

    disable_raw_mode();

    Ok(())
}

Expected behavior
We should see the MediaKeyCode events triggering, but they do not.

OS
Linux (Ubuntu 20.04)

Terminal/Console
Standard default GNOME terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant