I have read Gdk forum link which says that getting num lock state api is implemented since version 3.0. But I am using version 2.4 and I cannot update to version 3.0 as I need to support lower Linux version. Here is the discussion link:
http://mail.gnome.org/archives/commits-list/2010-July/msg00259.html
SO, is there any other way to get the num lock state using internal Linux command?
Regards, iSight
If you don't care about the Numlock state "while nothing is happening", and only when e.g. a keypress happens, the lowest overhead way is this.
For some
XKeyEvent *xke
For GDK, you might need something like Gdk.FilterFunc to get the
xevent
. Checkxevent->type
.I have checked the hard ware key code. Whenever num lock is on and pressed the number key at num pad i compare the hard ware key code which is universally constant to all manufacturer. Hence, I don't need to use ioctl.h header.
I did some sniffing around, and I found a possible implementation with
ioctl.h
that polls the keyboard state and tests that against a couple of flags.Take a look at this form post's implementation, and replace
K_CAPSLOCK
withK_NUMLOCK
*. It's pretty fugly, but it can easily be wrapped in a function and tucked away.*The reason for the replacement on the post was because of an old bug where caps lock and num lock were accidentally reversed. It should be fixed now.
You can use this linux command to do it
{ if (num_lock == 0) system("setleds -F +num"); else if num_lock == 1) ; //do nothing }
Sample code to get the NumLock state. Let
foo.c
be:Then this gives, tested with CentOS 5 on a Dell laptop:
Or you could do something with
popen("xset q | grep LED");
.The second bit of the mask is fairly common for NumLock, but I don't believe it is guaranteed.
Original answer: A good starting point is
xev
, available for about 20 years:And you can decode key events via: