I frequently work on pairing stations where there are multiple keyboards installed. I can use `setxkbmap` with `-device <ID>` to set the layout for a specific keyboard (using an [ID from `xinput`][1]), but often it's not obvious which keyboard I'm at. It would be better to avoid the back-and-forth of trying both keyboards, so I'd like to write a quick tool to get this information for `setxkbmap`. I'd expect a typical use case like the following:

 $ setxkbmap -device "$(get-keyboard-id)" -layout gb
 Press Enter to detect keyboard ID

Which interface provides this information on Linux? Ideally it should work without X, but that's not a requirement (there doesn't seem to be many tools which support this without X).

---

Findings so far:

- Linux *must* know which keyboard I'm typing on to support different layouts for multiple keyboards simultaneously.
- [`xinput`][2] → list.c → `list_xi2` → `XIQueryDevice` provides device IDs usable by `setxkbmap`.
- `showkey` and `xev` don't print keyboard IDs.
- There seems to be a connection between keyboards and `/dev/input/event*` devices, but it's not trivial (for example, I have a device ID 12, but no `/dev/input/event12`, and I have `/dev/input/event1` but no device ID 1).
- One almost possible solution is to run `xinput --test <ID> &` for each keyboard ID and see which one returns something first. The problem with that is figuring out which "keyboards" are *actually* keyboards:


 $ xinput | grep keyboard
 ⎣ Virtual core keyboard id=3 [master keyboard (2)]
 ↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
 ↳ Power Button id=6 [slave keyboard (3)]
 ↳ Video Bus id=7 [slave keyboard (3)]
 ↳ Power Button id=8 [slave keyboard (3)]
 ↳ Sleep Button id=9 [slave keyboard (3)]
 ↳ WebCam SC-13HDL10931N id=10 [slave keyboard (3)]
 ↳ AT Translated Set 2 keyboard id=11 [slave keyboard (3)]

 [1]: https://superuser.com/a/294034/2259
 [2]: http://www.x.org/releases/X11R7.7/src/everything/xinput-1.6.0.tar.gz