WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

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

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1511
Re: how to automatically get xinput id of keyboard?
« Reply #15 on: September 16, 2019, 09:11:44 AM »
Rich,

Apparently we can't rely on the word keyboard always appearing before the first left bracket. Here's from the OpenBSD partition I have laying around for these kinds of tests:

Code: [Select]
bruno@obsd:~353$ xinput list
⎡ Virtual core pointer                        id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                    id=4    [slave  pointer  (2)]
⎜   ↳ /dev/wsmouse0                                 id=7    [slave  pointer  (2)]
⎜   ↳ /dev/wsmouse                                  id=8    [slave  pointer  (2)]
⎣ Virtual core keyboard                       id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard                     id=5    [slave  keyboard (3)]
    ↳ /dev/wskbd                                      id=6    [slave  keyboard (3)]

The correct id that needs to be automatically found in this case is 6.

I came up with this, which works in all my GNU/Linux partitions, wife's wonky Sony Vaio with GNU/Linux, and also the OpenBSD test machine above:
Code: [Select]
master_id=$(LANG=C xinput list | grep -i 'master keyboard' | egrep -o 'id=[0-9]+' | egrep -o '[0-9]+')
keyboard_id=$(LANG=C xinput list $master_id | grep 'XIKeyClass' | egrep -o '[0-9]+')

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11634
Re: how to automatically get xinput id of keyboard?
« Reply #16 on: September 16, 2019, 09:50:31 AM »
Hi GNUser
... Apparently we can't rely on the word keyboard always appearing before the first left bracket. ...
It seems you also can't rely on the word  XIKeyClass  always appearing in the output. From TC4:
Code: [Select]
tc@box:~$ xinput list 3
Virtual core keyboard                           id=3    [master keyboard (2)]
        Reporting 1 classes:
                Class originated from: 6
                Keycodes supported: 248

tc@box:~$
TC4 dates back to 2012 so that may not be of interest. If you don't mind my being nosy, I have a couple of questions:
1. What are you working on that requires the keyboards XID?
2. What happens if a system has 2 keyboards?

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1511
Re: how to automatically get xinput id of keyboard?
« Reply #17 on: September 16, 2019, 10:09:55 AM »
No problem.

1. https://github.com/bdantas/iksilo
It is a little project for me and my Esperanto friends. The simplest way of typing in Esperanto is using the x-system, but a way of typing this way that works reliably on multiple UNIX-like operating systems is sorely lacking.

In order for the script to do its job, a crude keylogger (xinput test $keyboard_id) is built into the script, to keep track of the last two characters typed. I realize there are better ways to do this than with a shell script and X tools, but I'm trying to keep it as simple as possible.

2. If >1 keyboard then the autodetection won't work, but users savvy enough to have 2 keyboards can adjust the variable at the top of the script to match the id of the keyboard they want to use.

Sorry for all the trouble. This is way off-topic for Tiny Core, but I know TC users are smart so I wanted your input. We can close this thread now--what we have will work with non-ancient versions of xinput across multiple OSes  :)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11634
Re: how to automatically get xinput id of keyboard?
« Reply #18 on: September 16, 2019, 10:53:29 AM »
Hi GNUser
... In order for the script to do its job, a crude keylogger (xinput test $keyboard_id) is built into the script, ...
That's funny, I was going to ask if this was for a keylogger but opted to exercise some self control.

Quote
Sorry for all the trouble. This is way off-topic for Tiny Core,
It's no trouble at all. The topic involves scripting, text manipulation commands, internationalization, etc. As such, I consider it
relevant to this forum. Even if considered  off topic,  that's what this particular section of the forum is here for.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11634
Re: how to automatically get xinput id of keyboard?
« Reply #19 on: September 18, 2019, 01:21:40 PM »
Hi GNUser
Just an FYI, Xorg supports Esperanto. I see from your link you already know ablout  setxkbmap  and probably already found
this documentation:
https://www.x.org/releases/X11R7.5/doc/input/XKB-Config.html

An example:
Code: [Select]
setxkbmap -option esperanto:qwertyFound here:
https://esperanto.stackexchange.com/questions/378/how-do-i-type-the-esperanto-letters-with-accents-on-linux

A more advanced example:
Code: [Select]
setxkbmap -layout "us" -variant "alt-intl" -option "lv3:lwin_switch,esperanto:qwerty"Found in the paragraph titled  setxkbmap  here:
https://www.esperanto.org.nz/learn-and-use-esperanto/how-to-type-esperanto-characters/linux/

If you install  xkeyboard-config.tcz  you'll find  epo (short for Esperanto)  listed in:
/usr/local/share/X11/xkb/rules/evdev

And I think this is used for mapping the keyboard:
/usr/local/share/X11/xkb/symbols/epo

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1511
Re: how to automatically get xinput id of keyboard?
« Reply #20 on: September 18, 2019, 01:33:23 PM »
Rich, you are over the top in your desire to help! I hope someday I can reciprocate.

Yes, I am aware of those setxkbmap options (take a look at my script's add_keyboard_options function). The point of the script is to allow the Esperantist to not have to conform to the X.org way (Level3_shift--whatever key is chosen--plus c to produce ĉ, for example) but rather to use the common-among-Esperantists x-system (cx to produce ĉ, for example). For Esperantists who are used to the x-system, it is very fast to type that way (vs. having to stop, think, and move one's hands on the keyboard to type any other way).

Note that x-system in this context has nothing to do with X.org, it's just a coincidence that both have X in the name.






« Last Edit: September 18, 2019, 01:36:31 PM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11634
Re: how to automatically get xinput id of keyboard?
« Reply #21 on: September 18, 2019, 02:20:54 PM »
Hi GNUser
Truth of the matter is I stumbled upon the  Esperanto  reference while perusing the  /usr/local/share/X11/xkb  directory looking
for something else. I just  Googled  a couple of items to try to tie it all together.

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1511
Re: how to automatically get xinput id of keyboard?
« Reply #22 on: October 01, 2019, 08:24:14 AM »
Just a quick followup. I found a way to detect keyboard input without needing the keyboard id at all:

Code: [Select]
LANG=C xinput --test-xi2 --root | awk '
/RawKeyPress/ { relevant=1 }
relevant==1 && /detail/ { print $2; relevant=0 }
'

This makes for a good enough keylogger for my purposes. Note that if you put a pipe after the awk command, you need to disable awk's buffer with either { fflush() } or { system("") }. An example of that is here: https://github.com/bdantas/iksilo