Thread: uinput module keyboard events

  1. #1
    Registered User
    Join Date
    Feb 2023
    Posts
    4

    uinput module keyboard events

    hi, i'm trying to emulate keypress with uinput kernel module.
    i've found this code snippet in documentation 7. uinput module — The Linux Kernel documentation

    Code:
    #include <linux/uinput.h>
    
    void emit(int fd, int type, int code, int val)
    {
       struct input_event ie;
    
       ie.type = type;
       ie.code = code;
       ie.value = val;
       /* timestamp values below are ignored */
       ie.time.tv_sec = 0;
       ie.time.tv_usec = 0;
    
       write(fd, &ie, sizeof(ie));
    }
    
    int main(void)
    {
       struct uinput_setup usetup;
    
       int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    
    
       /*
        * The ioctls below will enable the device that is about to be
        * created, to pass key events, in this case the space key.
        */
       ioctl(fd, UI_SET_EVBIT, EV_KEY);
       ioctl(fd, UI_SET_KEYBIT, KEY_SPACE);
    
       memset(&usetup, 0, sizeof(usetup));
       usetup.id.bustype = BUS_USB;
       usetup.id.vendor = 0x1234; /* sample vendor */
       usetup.id.product = 0x5678; /* sample product */
       strcpy(usetup.name, "Example device");
    
       ioctl(fd, UI_DEV_SETUP, &usetup);
       ioctl(fd, UI_DEV_CREATE);
    
       /*
        * On UI_DEV_CREATE the kernel will create the device node for this
        * device. We are inserting a pause here so that userspace has time
        * to detect, initialize the new device, and can start listening to
        * the event, otherwise it will not notice the event we are about
        * to send. This pause is only needed in our example code!
        */
       sleep(1);
    
       /* Key press, report the event, send key release, and report again */
       emit(fd, EV_KEY, KEY_SPACE, 1);
       emit(fd, EV_SYN, SYN_REPORT, 0);
       emit(fd, EV_KEY, KEY_SPACE, 0);
       emit(fd, EV_SYN, SYN_REPORT, 0);
    
       /*
        * Give userspace some time to read the events before we destroy the
        * device with UI_DEV_DESTROY.
        */
       sleep(1);
    
       ioctl(fd, UI_DEV_DESTROY);
       close(fd);
    
       return 0;
    }
    after adding the headers missing, this snippet compiles ok, but I can't see the result of its work.

    the expected result is that the program should send a key press of "SPACE" system-wide, so i'm trying to catch it with putting the cursor in gedit after I start the program, but nothing happens

    please tell me what i'm doing wrong

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    open, write, ioctl can all return errors which you're blithely ignoring.

    Perhaps you could start by adding some robust error checking and reporting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2023
    Posts
    4
    sorry, these are my first steps in C and linux programming)
    i'm just trying to run the example in kernel manual

    Code:
        int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
        if (fd == -1) {
            fprintf(stderr, "Cannot open device: %s.\n", strerror(errno));
            return EXIT_FAILURE;
        }
    Code:
    int r = ioctl(fd, UI_SET_EVBIT, EV_KEY);
    if (r != 0) {
            fprintf(stderr, "Cannot enable device: %s.\n", strerror(errno));
            return EXIT_FAILURE;
        }
    is this a correct way to handle errors?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, looks good.

    Do you get any messages?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2023
    Posts
    4
    yes, the problem was I've launched the program with user privileges, so it couldn't open the device
    with sudo this snippet works

  6. #6
    Registered User
    Join Date
    Feb 2023
    Posts
    4
    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. simulate keyboard events on windows xp
    By xximranxx in forum Windows Programming
    Replies: 4
    Last Post: 04-26-2007, 05:43 PM
  3. Generating *Keyboard Events* thru programming ???
    By gameSTICKER in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2005, 11:03 AM
  4. Keyboard events, dialog boxes.
    By Brian in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2003, 10:20 AM
  5. tracking keyboard press/release events
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:21 PM

Tags for this Thread