Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hook into linux key event handling

Tags:

I want to hook into linux desktop key event handling.

Pressing CapsLock should enter some sort of command line.

Some of the commands I want to implement:

  • d/x: Delete from current cursor position until character x. (inspired by vi)
  • a: Goto to beginning of line, like pos1. (inspired by emacs).
  • k: Delete until end of line. (inspired by emacs).
  • ...

The commands should work in any text field: Browser, Mail Client, gnome terminal, ...

AFAIK low level xmodmap won't help me here.

Is something like this possible?

Where do I need to place the hook?

Current target platform is Ubuntu >= 14.04

Background: I want to keep my pointing fingers on F and J, and use the computer without looking at the keyboard. Works for A-Z since several years, but keys like Pos1/End are not easy to access.

Please leave a comment if you don't understand a part of this question. Thank you.

Update

This question is only about how to hook into the key event handling. The other stuff (command line) is a different topic. How can you catch for example CapsLock x?

Update2 I see there is no easy and straight forward solution. If you have no answer, but you know where I can find more help (like ask on mailing list FOO), please tell me.

Update3 Since some people do not understand what I want, I try to explain it: If I use emacs or bash I feel like being in control if the computer: it is like flying, with only very few movements I can tell the computer to do what I want. Editing text in webbrowser textarea, LibreOffice or using thunderbird makes this feeling go away. Cursor movements are cumbersome, it does not feel like flying. I want to control the desktop, not just a single application, and keep my pointing fingers on the F and J keys.

like image 661
guettli Avatar asked Dec 20 '14 15:12

guettli


2 Answers

UPDATE

Instead of telling the X server to ignore the device, you can use EVIOCGRAB ioctl, which I added to the program below.

You need to do the following things:

1.Make sure you have CONFIG_UINPUT module compiled and loaded. I believe Ubuntu already has it. If you don't see /dev/uinput device, try running modprobe -v uinput to load the module.

2.Run the following program as root and give it the path of the keyboard device, eg:

./process /dev/input/by-id/usb-Microsoft_Wired_Keyboard_600-event-kbd

The following program creates a fake input device called uinput-sample and forwards all events from a given input device to it. I adapted it from the sample given in http://thiemonge.org/getting-started-with-uinput

You can modify it to do things you want to do.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>

#define die(str, args...) do { \
        perror(str); \
        exit(EXIT_FAILURE); \
    } while(0)

int
main(int argc, char* argv[])
{
    int                    fdo, fdi;
    struct uinput_user_dev uidev;
    struct input_event     ev;
    int                    i;

    if(argc != 2) die("error: specify input device");

    fdo = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    if(fdo < 0) die("error: open");

    fdi = open(argv[1], O_RDONLY);
    if(fdi < 0) die("error: open");

    if(ioctl(fdi, EVIOCGRAB, 1) < 0) die("error: ioctl");

    if(ioctl(fdo, UI_SET_EVBIT, EV_SYN) < 0) die("error: ioctl");
    if(ioctl(fdo, UI_SET_EVBIT, EV_KEY) < 0) die("error: ioctl");
    if(ioctl(fdo, UI_SET_EVBIT, EV_MSC) < 0) die("error: ioctl");

    for(i = 0; i < KEY_MAX; ++i)
        if(ioctl(fdo, UI_SET_KEYBIT, i) < 0) die("error: ioctl");

    memset(&uidev, 0, sizeof(uidev));
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
    uidev.id.bustype = BUS_USB;
    uidev.id.vendor  = 0x1;
    uidev.id.product = 0x1;
    uidev.id.version = 1;

    if(write(fdo, &uidev, sizeof(uidev)) < 0) die("error: write");
    if(ioctl(fdo, UI_DEV_CREATE) < 0) die("error: ioctl");

    while(1)
    {
        if(read(fdi, &ev, sizeof(struct input_event)) < 0)
            die("error: read");

        ev.time.tv_sec = 0;
        ev.time.tv_usec = 0;

        if(write(fdo, &ev, sizeof(struct input_event)) < 0)
            die("error: write");
    }

    if(ioctl(fdo, UI_DEV_DESTROY) < 0) die("error: ioctl");

    close(fdi);
    close(fdo);

    return 0;
}
like image 60
Innocent Bystander Avatar answered Oct 26 '22 10:10

Innocent Bystander


The brute-force way would be to modyfy/rebuild xserver-xorg-input-evdev package and replace /usr/lib/xorg/modules/input/evdev_drv.so. I would start by trying to modify EvdevQueueKbdEvent() function in xf86-input-evdev-2.9.0/src/evdev.c. Doesn't look very elegant solution, but I think you'll get flexibility to modify keyboard event queue.

Less intrusive solution may be possible using XGRabKey() (some details here) and/or XGrabKeyboard().

Some info, which may be helpful here (regarding XTest extension).

like image 26
kestasx Avatar answered Oct 26 '22 10:10

kestasx