Skip to content

Instantly share code, notes, and snippets.

@WolfangAukang
Created May 11, 2017 18:01
Show Gist options
  • Save WolfangAukang/b4d7fa03efa97c5cfeacc542567dec7c to your computer and use it in GitHub Desktop.
Save WolfangAukang/b4d7fa03efa97c5cfeacc542567dec7c to your computer and use it in GitHub Desktop.
i3blocks extension that gets the indicated keyboard led status without using xset (useful for Sway, which uses Wayland)
#!/usr/bin/env python
from sys import argv
from evdev import InputDevice, ecodes
# usage: keystate-nonx [-h] [-i INPUT_DEVICE_CODE] [-oc OUTPUT_COLOR] [-ic INPUT_COLOR] [-b BLOCK_INSTANCE]
INSTANCE = "CAPS"
EVENT_NUMBER = "0"
COLOR_OFF = "#222222"
COLOR_ON = "#00FF00"
KEYBOARD_EVENT_PATH = "/dev/input/event"
#Returns a string with the input path
def keyboard_definition():
if (len(argv) == 1):
return ''.join([KEYBOARD_EVENT_PATH,EVENT_NUMBER])
else:
return ''.join([KEYBOARD_EVENT_PATH,argv[1]])
#Return the input device according to the keyboard definition
def set_input_device(keyboard_path):
return InputDevice(keyboard_path)
#Return the dictionary of leds available
def set_leds_dictionary(input_device):
full_led_list = input_device.capabilities(verbose=True)[('EV_LED', ecodes.EV_LED)]
return dict(full_led_list)
#Returns list of active leds on input device
def get_active_leds(input_device):
active_leds = input_device.leds()
return active_leds
def get_keystate(leds_dictionary, instance, active_leds):
uppercaseInstance = instance.upper()
if leds_dictionary['LED_'+uppercaseInstance+'L'] in active_leds:
return ''.join([uppercaseInstance,"\n",uppercaseInstance,"\n",COLOR_ON])
else:
return ''.join([uppercaseInstance,"\n",uppercaseInstance,"\n",COLOR_OFF])
if __name__=="__main__":
key_path = keyboard_definition()
input_device = set_input_device(key_path)
leds_dic = set_leds_dictionary(input_device)
active_leds = get_active_leds(input_device)
print(get_keystate(leds_dic, INSTANCE, active_leds))
@ForgottenUmbrella
Copy link

Thanks for creating this. I've taken the liberty of tweaking it to work with different colours and the NUM key, amongst other things. Here it is: https://github.com/ForgottenUmbrella/dotfiles/blob/master/i3/scripts/keyindicator-nox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment