Python - How to get current keylock status?

user1784064 picture user1784064 · Oct 29, 2012 · Viewed 9.4k times · Source

I'm attempting to write a simple programme that displays the current status of the different keylocks, but I'm unable to find a solution as to how to get the current status of them in Python. Thank you.

Answer

gvalkov picture gvalkov · Oct 29, 2012

If you can wait a day or two, I'll add this functionality to python-evdev and update this answer. It's probably going to look something along the lines of:

from evdev import InputDevice, ecodes

dev = InputDevice('/dev/input/eventX') # your keyboard device
dev.ledstates(verbose=True)
{ (0, 'LED_NUML')    : True,
  (1, 'LED_CAPSL')   : True,
  (2, 'LED_SCROLLL') : False}

Using xset, as mentioned by @ronak, is a lot easier since you don't have to have read permissions on any input devices. Unfortunately, it works only under X (and X in turn uses the evdev interface (at least on linux)).


Well, It took me long enough, but it's in. The interface for getting 'ON' LEDs ended up being:

>>> dev.leds()
[0, 1, 8, 9]

>>> dev.leds(verbose=True)
[('LED_NUML', 0), ('LED_CAPSL', 1), ('LED_MISC', 8), ('LED_MAIL', 9)]

Getting all available LEDs on a device:

>>> dev.capabilities()[ecodes.EV_LED]
[0, 1, 2]

>>> dev.capabilities(verbose=True)[('EV_LED', ecodes.EV_LED)]
[('LED_NUML', 0), ('LED_CAPSL', 1), ('LED_SCROLLL', 2)]