WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: how to automatically get xinput id of keyboard?  (Read 17103 times)

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
how to automatically get xinput id of keyboard?
« on: September 13, 2019, 10:05:26 AM »
I've written a shell script that needs to know the xinput id of the keyboard being used. In the case of my TC 10.1 and Devuan boxes, the id happens to be 9. However, in my OpenBSD box the id is 6.

I want to make the script portable and don't want to bother myself or other users with having to run xinput --list and then manually editing the keyboard id in the script. Is there a way to determine the id automatically? If the script has to wait for user to press a key so that script can find the id, that would be fine.

grepping output of xinput --list for "keyboard" won't work because there's more than one line with that word in it. Also, I don't want to assume that the system language is English.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: how to automatically get xinput id of keyboard?
« Reply #1 on: September 13, 2019, 12:07:06 PM »
Hi GNUser
The contents of  /proc/bus/input/devices  might have some interesting content:
Code: [Select]
tc@box:~$ cat /proc/bus/input/devices
I: Bus=0019 Vendor=0000 Product=0001 Version=0000
N: Name="Power Button"
P: Phys=PNP0C0C/button/input0
S: Sysfs=/devices/LNXSYSTM:00/device:00/PNP0C0C:00/input/input0
U: Uniq=
H: Handlers=kbd event0
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0019 Vendor=0000 Product=0001 Version=0000
N: Name="Power Button"
P: Phys=LNXPWRBN/button/input0
S: Sysfs=/devices/LNXSYSTM:00/LNXPWRBN:00/input/input1
U: Uniq=
H: Handlers=kbd event1
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0011 Vendor=0001 Product=0001 Version=ab41
N: Name="AT Translated Set 2 keyboard"
P: Phys=isa0060/serio0/input0
S: Sysfs=/devices/platform/i8042/serio0/input/input2
U: Uniq=
H: Handlers=sysrq kbd event2
B: PROP=0
B: EV=120013
B: KEY=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff fffffffe
B: MSC=10
B: LED=7

I: Bus=0011 Vendor=0002 Product=0006 Version=0006
N: Name="ImExPS/2 Logitech Explorer Mouse"
P: Phys=isa0060/serio1/input0
S: Sysfs=/devices/platform/i8042/serio1/input/input3
U: Uniq=
H: Handlers=mouse0 event3
B: PROP=0
B: EV=7
B: KEY=1f0000 0 0 0 0 0 0 0 0
B: REL=143

I: Bus=0010 Vendor=001f Product=0001 Version=0100
N: Name="PC Speaker"
P: Phys=isa0061/input0
S: Sysfs=/devices/platform/pcspkr/input/input4
U: Uniq=
H: Handlers=kbd event4
B: PROP=0
B: EV=40001
B: SND=6

tc@box:~$

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: how to automatically get xinput id of keyboard?
« Reply #2 on: September 13, 2019, 01:18:35 PM »
Thanks for that, Rich. Interesting. However, I don't see the xinput ids listed in there.

Meanwhile I found this https://unix.stackexchange.com/questions/89538/how-to-tell-which-keyboard-was-used-to-press-a-key

The above comes close, but having to ship a C program with my shell script is not an option. I need to figure out a way to find the id using native X tools, even if the script has to wait for a key to be pressed.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: how to automatically get xinput id of keyboard?
« Reply #3 on: September 13, 2019, 01:56:13 PM »
Hi GNUser
This should get you into the ballpark:
Code: [Select]
tc@E310:~$ cat /proc/bus/input/devices | grep -i keyboard
N: Name="DELL DELL USB Keyboard"
tc@E310:~$ xinput list --id-only "DELL DELL USB Keyboard"
9
tc@E310:~$

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: how to automatically get xinput id of keyboard?
« Reply #4 on: September 13, 2019, 07:06:45 PM »
Beautiful! Thank you. That worked in every Linux box I tried.

I also found this solution, which works in the BSDs as well because it does not rely on info provided by the linux kernel:

Code: [Select]
xinput --list --long | grep XIKeyClass | head -n 1 | egrep -o '[0-9]+'

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: how to automatically get xinput id of keyboard?
« Reply #5 on: September 13, 2019, 08:48:41 PM »
Hi GNUser
This one picks out the line for the actual keyboard:
Code: [Select]
tc@E310:~$ xinput --list | grep -i keyboard | grep -iv "Virtual core" | grep -iv Button
    â³ DELL DELL USB Keyboard                          id=9    [slave  keyboard (3)]
tc@E310:~$
And it's backward compatible with TC4:
Code: [Select]
tc@box:~$ xinput --list | grep -i keyboard | grep -iv "Virtual core" | grep -iv Button
    â³ Keyboard0                                       id=6    [slave  keyboard (3)]
tc@box:~$

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 10957
Re: how to automatically get xinput id of keyboard?
« Reply #6 on: September 14, 2019, 12:19:21 AM »
Regarding the system language, you can run "LANG=C xinput ..." to get English output.
The only barriers that can stop you are the ones you create yourself.

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: how to automatically get xinput id of keyboard?
« Reply #7 on: September 14, 2019, 04:16:00 AM »
Thank you both. I think we're getting warmer. Does this work on all your test systems?

Code: [Select]
LANG=C xinput --list | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
« Last Edit: September 14, 2019, 04:20:15 AM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: how to automatically get xinput id of keyboard?
« Reply #8 on: September 14, 2019, 05:02:55 AM »
Hi GNUser
... Does this work on all your test systems? ...
Yes it does. TC10:
Code: [Select]
tc@E310:~$ LANG=C xinput --list | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
9
tc@E310:~$
TC4:
Code: [Select]
tc@box:~$ LANG=C xinput --list | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
6
tc@box:~$

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: how to automatically get xinput id of keyboard?
« Reply #9 on: September 14, 2019, 06:35:44 AM »
Darn, on my wife's Sony Vaio I get some stray hits:

Code: [Select]
eileen@vaio:~$ LANG=C xinput --list | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
7
10
12
14
eileen@vaio:~$ LANG=C xinput --list | grep -i keyboard | egrep -iv 'virtual|video|button|bus'
    ↳ Sony Vaio Keys                          id=7 [slave  keyboard (3)]
    ↳ USB 2.0 Camera: USB 2.0 Camera          id=10 [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard            id=12 [slave  keyboard (3)]
    ↳ Sony Vaio Jogdial                        id=14 [slave  keyboard (3)]

The correct id on her system is 12. There must be a universal solution, but we're not there yet.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: how to automatically get xinput id of keyboard?
« Reply #10 on: September 14, 2019, 08:15:19 AM »
Hi GNUser
Those  master  and  slave  messages in the square brackets at the end of each line are creating a lot of unnecessary noise:
Code: [Select]
tc@box:~$ xinput --list
â¡ Virtual core pointer                        id=2    [master pointer  (3)]
â   â³ Virtual core XTEST pointer                    id=4    [slave  pointer  (2)]
â   â³ Mouse0                                        id=7    [slave  pointer  (2)]
⣠Virtual core keyboard                       id=3    [master keyboard (2)]
    â³ Virtual core XTEST keyboard                     id=5    [slave  keyboard (3)]
    â³ Keyboard0                                       id=6    [slave  keyboard (3)]
tc@box:~$

Time to cut them out:
Code: [Select]
tc@box:~$ xinput --list | cut -d\[ -f1
â¡ Virtual core pointer                        id=2   
â   â³ Virtual core XTEST pointer                    id=4   
â   â³ Mouse0                                        id=7   
⣠Virtual core keyboard                       id=3   
    â³ Virtual core XTEST keyboard                     id=5   
    â³ Keyboard0                                       id=6   
tc@box:~$
The  -d\[  sets the field delimiter to  [  and the  -f1  says to output field number 1 (everything before the left square bracket).

Try this:
Code: [Select]
tc@box:~$ LANG=C xinput --list | cut -d\[ -f1 | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
6
tc@box:~$
« Last Edit: September 14, 2019, 08:18:09 AM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: how to automatically get xinput id of keyboard?
« Reply #11 on: September 14, 2019, 08:27:51 AM »
Hi GNUser
Here's a brief writeup that provides some insight to the various devices listed by  xinput:
https://www.linux.org/threads/cool-tricks-with-xinput-device.10345/

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: how to automatically get xinput id of keyboard?
« Reply #12 on: September 14, 2019, 04:25:54 PM »
Thank you very much, Rich. Your last suggestion worked on all systems I tested, even on wife's Vaio:

Code: [Select]
eileen@vaio:~$ LANG=C xinput --list | cut -d\[ -f1 | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
12

Cutting out the slave/master column at far right was key. Very nice. Thank you.

I will look over the link and educate myself.

Offline xor

  • Hero Member
  • *****
  • Posts: 1259
Re: how to automatically get xinput id of keyboard?
« Reply #13 on: September 16, 2019, 12:31:26 AM »
Thank you very much, Rich. Your last suggestion worked on all systems I tested, even on wife's Vaio:

Code: [Select]
eileen@vaio:~$ LANG=C xinput --list | cut -d\[ -f1 | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
12

Cutting out the slave/master column at far right was key. Very nice. Thank you.

I will look over the link and educate myself.

Thanks to this feature; It is nice to have the KMAPS setting of languages other than English set automatically :)

will this be standard in the next TCL version in particular!?

Offline xor

  • Hero Member
  • *****
  • Posts: 1259
Re: how to automatically get xinput id of keyboard?
« Reply #14 on: September 16, 2019, 01:17:53 AM »
Thank you very much, Rich. Your last suggestion worked on all systems I tested, even on wife's Vaio:

Code: [Select]
eileen@vaio:~$ LANG=C xinput --list | cut -d\[ -f1 | grep -i keyboard | egrep -iv 'virtual|video|button|bus' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+'
12

Cutting out the slave/master column at far right was key. Very nice. Thank you.

I will look over the link and educate myself.

for non-English keyboards,
(automatic keyboard recognition feature),
Will there be a new standard feature for TCL !?
I want this feature to be a standard feature.

In my country "Q" keyboard and "F" keyboard are used equally.

automatically localized keyboard settings,
Provides a universal convenience for all local regions! :)