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

grab simulates Enter keypresses on linux x11 #101

Open
JakeSaphhire opened this issue Feb 18, 2023 · 4 comments
Open

grab simulates Enter keypresses on linux x11 #101

JakeSaphhire opened this issue Feb 18, 2023 · 4 comments

Comments

@JakeSaphhire
Copy link

JakeSaphhire commented Feb 18, 2023

Running grab correctly captures KeyEvents globally but it also simulates Enter keypresses globally despite no instructions to do so. Here's the minimum example to reproduce this.

use rdev::{grab, Event, EventType, Key};
use std::process;


fn main() {
    println!("Hello, world!");
    if let Err(error) = grab(callback) {
        println!("Error: {:?}", error)
    }
    
}

fn callback(event: Event) -> Option<Event> {
    match event.event_type {
        EventType::KeyPress(Key::Escape) => {process::exit(1); None},
        EventType::KeyPress(key) => {println!("Keypressed: {:?}", key); None},
        _ => Some(event),
    }
}

With
Linux 6.1.12-arch1-1
rustc 1.60.0 (7737e0b5c 2022-04-04)

@Narsil
Copy link
Owner

Narsil commented Feb 18, 2023

Not sure I understand. You mean the Enter keypress is not ignored ? (Your code is capturing Escape, not Enter, is that it ?)

@JakeSaphhire
Copy link
Author

Hello and thanks for the speedy response :)

The problem is that basically the grab function works and it correctly captures and registers keyboard events (including pressing the Esc key to close the program) but everytime the grab function runs and matches an Event, it sends an Enter keypress. You can see side effects of that Enter in this video (terminal scrolling, google searching again, firefox reloading etc.)

2023-02-18 15-12-21

@Narsil
Copy link
Owner

Narsil commented Feb 19, 2023

The problem is that you're using std::process::exit which kills the process without letting it clean up, meaning the end state is most likely UB.

One solution would be something like that:

use rdev::{grab, Event, EventType, Key};
use std::process;

fn main() {
    println!("Hello, world!");
    if let Err(error) = grab(callback) {
        println!("Error: {:?}", error)
    }
}

static mut STOP: bool = true;

fn callback(event: Event) -> Option<Event> {
    unsafe {
        println!("Stop {:?}", STOP);
        match (STOP, event.event_type) {
            (false, key) => {
                println!("Keypressed through: {:?}", key);
                Some(event)
            }
            (true, EventType::KeyPress(Key::Escape)) => {
                // process::exit(1);
                STOP = false;
                None
            }
            (true, EventType::KeyPress(key)) => {
                println!("Keypressed: {:?}", key);
                None
            }
            _ => Some(event),
        }
    }
}

If you want to actualy kill the process you need to find a way to do a proper keyboard interrupt or similar.

@jersou
Copy link

jersou commented Apr 7, 2023

I had the same problem: the state of the devices is kept at startup (or something like that).
A workaround is to wait until the Enter key is released before starting the grab. An easy check :

sleep 0.5 ; sudo ./target/debug/psylle

I use this workaround here : https://github.com/jersou/mouse-actions/blob/7bd717d32408d1b836e031531f1d051b51957e04/src/main.rs#L33

cyrinux added a commit to cyrinux/push2talk that referenced this issue Oct 24, 2023
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

3 participants