I've created a Python program in Linux that would blink the NumLock LED. The code is shown below:
import fcntl
import os
import time
# Define variables for keyboard and NUM_LOCK LED
KDSETLED = 0x4B32
NUM_LED = 0x02
# Open the "keyboard console" for LED toggling
console_fd = os.open('/dev/console', os.O_NOCTTY)
def blink(tme, cnt):
secs = tme/cnt
for i in range(cnt):
fcntl.ioctl(console_fd, KDSETLED, 0)
time.sleep(secs)
fcntl.ioctl(console_fd, KDSETLED, NUM_LED)
if i < cnt - 1:
time.sleep(secs)
blink(0.5,1)
If it looks vaguly familiar, it's because it came from a similar post here.
It blinks correctly for one of my Power Up! USB Numeric Keypad (Part No.: G54-41403). However, while the program does run without any errors, the LEDs do not blink for my older keyboards (I've also verified that the NUM LOCK does properly). The older keyboards are a Targus Numeric Keypad with USB Hub (Model PAUK10U note that I couldn't find my older model online), and a Rlip Ergonomic Keypad KNP-180.
I checked the Python module python-keyboardleds
(link here), and I found that the program accesses the keyboard directly. Specifically, while I access it through /dev/console
, the program accesses it through the /dev/input/by-path/
. I tried changing the code so that I open the first keyboard on the list, and the same results occurred as before (where there were no errors, and the LED didn't blink on the other keyboard models). The code I made is shown below:
#LEDToggle.py
import argparse
import glob
import grp
import os
import pwd
import re
import subprocess
import time
import fcntl
import keyboardleds
def drop_privileges():
uid = pwd.getpwnam('nobody').pw_uid
gid = grp.getgrnam('nogroup').gr_gid
os.setgid(gid)
os.setuid(uid)
def parse_args():
led_names = list(t + '-lock' for t in ('caps', 'num', 'scroll'))
ap = argparse.ArgumentParser()
ap.add_argument('--led', choices=led_names, default=led_names[1],
help='keyboard LED to use',
)
return ap.parse_args()
def blink(tme, cnt):
secs = tme/cnt
options = parse_args()
event_device = glob.glob('/dev/input/by-path/*-event-kbd')[0]
ledkit = keyboardleds.LedKit(event_device)
drop_privileges()
#led = getattr(ledkit, 'num-lock')
led = getattr(ledkit, options.led.replace('-', '_'))
for i in range(cnt):
led.reset()
print "off"
time.sleep(secs)
led.set()
print "on"
if i < cnt - 1:
time.sleep(secs)
blink(5,5)
After research, I suspected that some keyboards are not being called correctly when accessing the /dev/console
. When I look into /dev/input/by-path/
, I find that the working keyboard is named platform-bcm2708_usb-usb-0:1.3:1.0-event-kbd
. Then when I try to connect one of the two non-working keyboards, they have the same name.
I've also tried using the setleds
commands in the kernel, as well as calling both the console and tty7
to communicate with the keyboard. Both of these did the same thing; they worked on the Power Up! keyboard and (without any errors) didn't blink on the other two keyboards.
I feel like I am not communicating with these older keyboards correctly. Other than from what I tried, is there any other way to toggle the keyboard LEDs?