3

I want some way to know if the Capslock is active or not, thought I can use xet for this purpose, using pipe, by popen('xset -q | grep Capslock') I am able to find out, but I want some way by which there is no use of the commands, in the C program, is there any way to know this. One more thing I want to ask in this context, xset doens't work in the console mode in linux, I do alt+ctrl+f1 then login there and if try to run xset -q this will throw error, perhaps this can't communicate with the XWindows in console, so what solution can be for this case.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Nimit Bhardwaj
  • 827
  • 9
  • 19

2 Answers2

2

I want some way to know if the Capslock is active or not

You probably want XkbGetIndicatorState. For instance:

#include <stdio.h>
#include <stdlib.h>
#include <X11/XKBlib.h>

/* Compile this with -lX11 */

int main ()
{
  Display *display;
  Status status;
  unsigned state;

  display = XOpenDisplay (getenv ("DISPLAY"));
  if (!display)
    return 1;

  if (XkbGetIndicatorState (display, XkbUseCoreKbd, &state) != Success)
    return 2;

  printf ("Caps Lock is %s\n", (state & 1) ? "on" : "off");
  return 0;
}

Alternatively, you can go with the same approach that is used in xset and use XkbGetNamedIndicator which is a more general function.

undercat
  • 529
  • 5
  • 17
1

Download the source to xset and see how it does things. It's not black magic. It will give you the functions to call to get/set the things you want. For xset to work, it must be invoked under the Window manager, so it can't be done from a VT console.

For the VT, from man 2 ioctl_console, you can use the KDGKBLED and KDSKBLED ioctls to get/set the flags.

Craig Estey
  • 30,627
  • 4
  • 24
  • 48