How to detect keyboard events without a window?

I intended to build a project that requires listening to key-pressed and key-released events and was using crate piston, but found it wired to require a window, and it can only detect events within the window. In the project a window limits the listener and is useless, so how can I detect keyboard events without a window?
the listener with a window:

use piston::WindowSettings;
fn main() {
    let settings = WindowSettings::new("Roguelike", [0; 2]).exit_on_esc(false);
    let mut window :PistonWindow= settings.build().expect("Could not create window");
    let mut events = Events::new(EventSettings::new());
    while let Some(e)=events.next(&mut window) {
        if let Some(k) = e.button_args() {
            if k.state == ButtonState::Press {
                match k.button {
                    Button::Keyboard(Key::Up) => println!("up!"),
                    //...
                    _ => (),
                }
            }
        }
    }
}

Thank you for any thoughts!

I don't know anything about Piston; is that a requirement for your use case?

The winit crate can report device events like keyboard keys without a focused window.

1 Like

Oh, thanks a lot! I'm trying it out.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.