Reputation: 25
Hello guys I'm trying parse xinput stdout to find the right device to disable the keyboard. So this is the code that I have but it looks sloppy and I was wondering if there was a better way of doing it.
xinput -list --short | grep -E "keyboard.*slave" | awk 'NR==1{print substr($0,7,41)}' | awk '{$1=$1}1'
I do not know how long the substring needs to be so I include 41 as the index to encompass the maximum length of the potential device names. This results in a white space at the end that needs to be trimmed so I pass it through another awk pipe. I would like to know if the last 2 awk calls can be condensed into 1
Upvotes: 1
Views: 1021
Reputation: 6527
You can use sub()
to substitute the white space away, and awk
can do the pattern match for you, too:
xinput -list --short | awk '
/keyboard.*slave/ {
s = substr($0, 7, 41);
sub(/ *$/, "", s);
print s;
exit;
}'
That would stop after the first matching line, which I suppose you wanted, since the original only acted on the first record read by awk
.
So,
$ cat keyb
mouse mouse something
keyboard slave foo
keyboard slave bar
$ awk '/keyboard.*slave/ { s = substr($0, 7, 41); sub(/ *$/, "", s); print s; exit; }' < keyb
rd slave foo
Upvotes: 2
Reputation: 6335
As an alternative to disable your keyboard you can use xinput directly.
The output of xinput is like this:
$ xinput list
Virtual core pointer id=2 [master pointer (3)]
↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
↳ VirtualBox mouse integration id=9 [slave pointer (2)]
↳ ImExPS/2 Generic Explorer Mouse id=11 [slave pointer (2)]
Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Sleep Button id=7 [slave keyboard (3)]
↳ Video Bus id=8 [slave keyboard (3)]
↳ AT Translated Set 2 keyboard id=10 [slave keyboard (3)]
The keyboard description i.e AT Translated Set 2 keyboard
is always the same. What is different is the id=10 (different id can be assigned by the system across reboots).
As a result you can get the id of your keyboard like
$ xinput list --id-only "AT Translated Set 2 keyboard"
10
And you can then disable your keyboard like
$ kid=$(xinput list --id-only "AT Translated Set 2 keyboard")
$ xinput disable "$kid" #use xinput enable to enable it.
Or even you can use device description directly with xinput disable:
$ xinput disable "AT Translated Set 2 keyboard"
As a result , since device description is always the same , you don't need any kind of text processing to identify and disable your keyboard.
Upvotes: 1