Skip navigation


#include <X11/Xlib.h>
#include <X11/XKBlib.h>

const int CapsLock = 2, NumLock = 16, ScrollLock = 1;

void setLeds(int leds) {
    Display *dpy = XOpenDisplay(0);
    XKeyboardControl values;
    values.led_mode = leds & ScrollLock ? LedModeOn : LedModeOff;
    values.led = 3;
    XChangeKeyboardControl(dpy, KBLedMode, &values);
    XkbLockModifiers(dpy, XkbUseCoreKbd, CapsLock | NumLock,
        leds & (CapsLock | NumLock) );
    XFlush(dpy);
    XCloseDisplay(dpy);
}

int main() {
    setLeds(CapsLock | NumLock | ScrollLock);
}

5 Comments

  1. This worked for me, but crashed after a while. I needed to add “XCloseDisplay(dpy)” at the end to avoid too many connections and a seg fault.

  2. Thanks for the comment Ed. I did fix that in our production app some months ago but forgot to update the blogpost :)

    • Many thanks for your code. I’ve been wondering how to accomplish this feat. Now I have some free control lines to use so my keyboard emulator has I AND O. :-)

  3. At least on Ubuntu 12.04 (where the ledcontrol package has been deleted) you can also use xset (from the x11-xserver-utils package), e.g.

    xset led named “Scroll Lock”
    xset -led named “Scroll Lock”

    • xset led named “Scroll Lock” is work
      xset led named “Caps Lock” not work

      :(


4 Trackbacks/Pingbacks

  1. By » Linux: Enable num-lock as default in Linux on 11 Jun 2015 at 9:17 am

    […] KP_1 and so on). If you want to retain the modifiers but make Num Lock default on, you can write a small program to call XKbLockModifiers to set the modifier inside X and XChangeKeyboardControl to set the […]

  2. […] provides the Drive LED functionality on the Dell XPS 13. (Thanks to P.I. Engineering, Manwe and Stefo for their […]

  3. […] KP_1 and so on). If you want to retain the modifiers but make Num Lock default on, you can write a small program to call XKbLockModifiers to set the modifier inside X and XChangeKeyboardControl to set the […]

  4. […] From a suggestion from Mark Plotnik, and based on code found here, I wrote and compiled the […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: