Skip to content

Instantly share code, notes, and snippets.

@cwervo
Created November 8, 2023 21:23
Show Gist options
  • Save cwervo/7054ca1a74a7587a164018634a312daa to your computer and use it in GitHub Desktop.
Save cwervo/7054ca1a74a7587a164018634a312daa to your computer and use it in GitHub Desktop.
A script for figuring out what keyboards are currently attached in Linux

Make this executable (e.g. chmod +x listKeyboards.tcl) and then ./listKeyboards.tcl

Sample output:

folk@folk0:~$ ./listKeyboards.tcl
Working ...
-- Keyboard found: /dev/input/event6 DEVPATH=/devices/pci0000:00/0000:00:01.1/0000:02:00.0/usb3/3-2/3-2:1.2/0003:046D:C52B.000C/00
03:046D:404B. 000D/input/input67/event6
-- Keyboard found: /dev/input/event7 | DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-11/1-11:1.2/0003:046D:52.0006/0003:046D:404
B.0009/input/input35 /event 7
-- Keyboard found: /dev/input /event3 DEVPATH=/devices /platform/i8042/serio/input/input3/event3
folk@folk-convivial:~/folk$ ./listKeyboards.tcl
Working ..,
-- Keyboard found: /dev/input /event13 DEVPATH=/devices/pci0000:00/0000:00:08.1/0000:04:00.3/usb1/1-3/1
-3:1.0/0003:046D:C31C.0001/input /input15/event13
Done listing keyboards.
#!/usr/bin/env tclsh8.6
# Andrés Cuervo (2023-11-08)
# This Tcl script lists all keyboard devices in the /dev/input/ directory.
puts "Working ..."
proc udevadm_properties {device} {
return [exec udevadm info --query=property --name=$device]
}
proc devpath {device} {
return [exec udevadm info --query=property --name=$device | grep DEVPATH=]
}
# Function to check if the device is a keyboard
proc is_keyboard {device} {
# Get device properties using udevadm
set udev_info [udevadm_properties $device]
# Check for the property that identifies the device as a keyboard
return [string match *ID_INPUT_KEYBOARD=1* $udev_info]
}
# Iterate over all event devices and check if they are keyboards
set eventInputs [glob -nocomplain /dev/input/event*]
foreach device $eventInputs {
if {[is_keyboard $device]} {
puts "-- Keyboard found: $device | [devpath $device]\n-------\n"
}
}
puts "Done listing keyboards."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment