For example, is the keycode of left control always 37? Where can I find a list of such keycodes?
1 Answer
No - it depends upon the keyboard configuration (ultimately upon hardware). If you're using the same hardware all the time, this is not an issue.
xterm uses keycodes in the modifyOtherKeys
feature. Some of the background (and examples for several keyboard types) are given in the XTerm – “Other” Modified Keys page.
The tables on that page are generated by modify-keys.pl, a script which imitates xterm's behavior for a given X keyboard configuration. It gets some of the information from "localectl status", and some from setkbmap (in more or less readable form). Ultimately that comes from tables which are compiled from other sources, but those utilities were the easiest way to get usable information from the X keyboard configuration.
Checking my memory, I made a script to show the details of the variations in those tables:
#!/bin/sh
for kbd in apple altgr-intl pc105
do
for lang in us gb fr
do
setxkbmap -model $kbd -layout $lang -print | \
xkbcomp - -C -o - >/tmp/$kbd-$lang.log 2>/dev/null
done
done
The X keycode happens to be the index into the symCache
array defined in those source-files, e.g., around here:
#define NUM_SYMBOLS 358
static KeySym symCache[NUM_SYMBOLS]= {
But the table-size changes with different configurations:
altgr-intl-fr.log:#define NUM_SYMBOLS 347
altgr-intl-gb.log:#define NUM_SYMBOLS 348
altgr-intl-us.log:#define NUM_SYMBOLS 254
apple-fr.log:#define NUM_SYMBOLS 358
apple-gb.log:#define NUM_SYMBOLS 359
apple-us.log:#define NUM_SYMBOLS 265
pc105-fr.log:#define NUM_SYMBOLS 375
pc105-gb.log:#define NUM_SYMBOLS 376
pc105-us.log:#define NUM_SYMBOLS 282
and the left/right control symbols appear at different offsets in some of the tables:
altgr-intl-fr.log: XK_Control_L, XK_Super_L, XK_Shift_R, XK_Control_R,
altgr-intl-gb.log: XK_Control_L, XK_Super_L, XK_Shift_R, XK_Control_R,
altgr-intl-us.log: XK_Num_Lock, XK_Shift_L, XK_Control_L, XK_Super_L,
apple-fr.log: XK_Control_L, XK_Super_L, XK_Shift_R, XK_Control_R,
apple-gb.log: XK_Control_L, XK_Super_L, XK_Shift_R, XK_Control_R,
apple-us.log: XK_Num_Lock, XK_Shift_L, XK_Control_L, XK_Super_L,
pc105-fr.log: XK_Control_L, XK_q, XK_Q, XK_at,
pc105-gb.log: XK_Control_L, XK_a, XK_A, XK_ae,
pc105-us.log: XK_Num_Lock, XK_Shift_L, XK_Control_L, XK_Super_L,
So I'd not expect those keycodes to always be the same values for all keyboard configurations.
The comment about xev
mentions a way to see the keycodes for the current configuration. On mine, the left control is 67 (not 37):
KeyRelease event, serial 34, synthetic NO, window 0xa00001,
root 0x111, subw 0xa00002, time 51155669, (23,30), root:(43,73),
state 0x4, keycode 67 (keysym 0xffe3, Control_L), same_screen YES,
XLookupString gives 0 bytes:
XFilterEvent returns: False
KeyPress event, serial 34, synthetic NO, window 0xa00001,
root 0x111, subw 0xa00002, time 51156901, (23,30), root:(43,73),
state 0x0, keycode 70 (keysym 0xffe4, Control_R), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False
-
I just need the codes for a script that is supposed to detect if a modifier key is pressed from
xinput
's output. And it would be great if the script would work on other people's computers, too.– jarnoJul 4, 2020 at 8:53 -
"xinput test" gives some numbers, but those aren't X keycodes. You might find this answer helpful, though some of it is incorrect... Jul 4, 2020 at 11:10
-
-
sorry - I was looking at the keysyms column of a table. xev is (agreed) giving consistent results for keycodes Jul 4, 2020 at 21:50