# How does a keystroke reach the (X) software?

## ManDay

I'm having troubles making a key work.

If I were to implement the path a keystroke took until it reaches the application, I would do it like that:

Kernel translates Scancode into a Keycode by the Keyboard driver, which knows which scancode corresponds to which key for a specific keyboard hardware.

Kernel then translates the Keycode to a Keyvalue according to a specific keymap for the specific keyboard layout (one keyboard hardware may thus have multiple layouts).

X11 gets a Keycode and composes (if necessary) it into a Keysym, such as "é" or "ô" (composed) or "Esc", "k", "XF86Home".

The software can then either listen to keysyms or to keycodes, depending on what they want to do.

To my understanding the kernel part goes as I described it, but I don't understand by which logic X11 currently works. There seems to be a truckload of tables in X which translate keycodes back-and-forth without any apparent advantage of doing so.

There are /usr/include/X11 and /usr/share/X11/xkb, which again branch out into a million files which do all sorts of mapping. Not only am I not able to see any sense in this, but I can't even get it to work from which I can infer.

I tried to make scancode e02d be recognized as XF86Menu. For that, I found a thing like

```
/usr/share/X11/xkb/symbols/inet:    key <I147>   {      [ XF86MenuKB            ]       };
```

and

```
/usr/share/X11/xkb/keycodes/evdev:       <I147> = 147;   // #define KEY_MENU
```

So I ran "setkeycodes e02d 147" but then the key maps to XF86Xfer. Can anyone explain this?

----------

## jrussia

In my /usr/share/X11/xkb/keycodes/evdev I have

<I147> = 147; // #define KEY_MENU        139

and 

<I155> = 155; // #define KEY_XFER         147

so judging by your results, I would guess that the correct keycode for you would be 139.

----------

## BillWho

ManDay,

x11-apps/xev will display X events. 

```
xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'
```

will display the key's scancode 

Another application you might want to look into is x11-apps/xmodmap  :Wink: 

----------

