Giter VIP home page Giter VIP logo

Comments (3)

gvalkov avatar gvalkov commented on August 11, 2024 1

Take this with a grain of salt - I think this happens because your terminal receives a key press event (1), but never the corresponding key release (2) event. The same would happen even if you wrote your program in C:

#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <poll.h>

int main(int argc, char *argv[])
{
    int fd = open("/dev/input/event2", O_RDONLY | O_NONBLOCK);

    struct pollfd fds = {fd, POLLIN, POLLIN};
    struct input_event event;

    char cmd[50];
    sprintf(cmd, "sleep 5 && kill %d &", (int)getpid());
    system(cmd); // just in case

    while (poll(&fds, 1, -1)) {
        int n = read(fd, &event, sizeof(event));

        if (event.type == EV_KEY) {
            if ((event.code == KEY_LEFTCTRL) && (event.value == 1))
                ioctl(fd, EVIOCGRAB, 1);
            if ((event.code == KEY_LEFTCTRL) && (event.value == 0))
                ioctl(fd, EVIOCGRAB, 0);
        }

    return 0;
}

You can inject a few events into the input stream to prevent this:

ui = UInput()
for ev in dev.read_loop():
    if ev.code == KEY_LEFTCTRL and ev.value == 1:
        ui.write_event(ev)
        ui.write(EV_KEY, KEY_LEFTCTRL, 0)
        ui.syn
        dev.grab()
    ...

from python-evdev.

moigagoo avatar moigagoo commented on August 11, 2024

Actually, that is what I did in the first place, just removed the ui stuff for the sake of clarity in the example.

You see, the odd thing is, the ungrab command seems to be executed all right: ”Ungrabβ€œ is printed and if you try to ungrab it again (e.g. after the loop), you get an error (which means the device is already ungrabbed). I believe, the problem is in the grab/ungrab itself. The release event is also caught (just printing all the events proves it).

I will try to follow your advice though. Thanks a lot for fast and useful comments!

from python-evdev.

moigagoo avatar moigagoo commented on August 11, 2024

Thanks a million!

Even though I don't get why injecting a 'release' event right after a 'press' one would help, it did :-)

Thanks for assistance, and thanks for the module, great work that saved my day.

from python-evdev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.