I am using Ubuntu 12.04 and i need to control my customized USB keyboard keys & LEDs. The Keys are working well. When I tried with a standard keyboard(having NUM, SCROLL, CAPS lock LEDs), i couldnt succeed with writing LED status bits.

I dont want to use console based kb ioctl calls to control LEDs. Is there any way to control directly using ioctl or write function at driver space.

Below is the code i tried:

kb = open(/dev/input/event2, O_RDWR );
char kbname [256] = "key-x123";
char LedStatus = 0;

ioctl (kb, EVIOCGNAME (sizeof (kbname)), kbname);

ioctl (kb, EVIOCGBIT (EV_LED,sizeof (LedStatus)), &LedStatus);

ioctl (kb, EVIOCGLED (sizeof (LedStatus)), &LedStatus);

All these calls are working well and i could reak keys. But unable to set any LEDs. Do not find any macros like EVIOCGLED to set LEDs through ioctl(2).

I tried below code as well which throws "invalid parameter" error.

struct input_event event;
event.type  = EV_LED;
event.value = 7;

  if (write (kb, &event, sizeof (struct input_event)) < 0)
  • setleds can do that from the command line. Can you call out to your shell from your program? – charlesbridge Jul 18 '13 at 16:35
  • i need to control from a program directly. now its working well – Sambath Jul 19 '13 at 8:01

It's working fine with code below:

event.type  = EV_LED;
event.code  = LED_CAPSL; /* or any other LEDs */
event.value = 0; /* or 1 */

if (write (kb, &event, sizeof (struct input_event)) < 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.