PDA

View Full Version : Kana Lock Detection



dswartz
12th September 2011, 16:18
Hi, I'm very new to Qt, but I have been recently been assigned to a project involving Qt & X11 in Linux (a flavor of Ubuntu). One of my tasks is to write a function to detect the state of caps lock, number lock, scroll lock, and Kana lock. So far, I have been able to detect caps, number, and scroll lock, but can't seem to figure out Kana lock. Does anyone know how to use XkbGetIndicatorState() to detect Kana lock? If not, does anyone know a better, or different, method that is capable of detecting the state of all four?


My code for the other three is:

//---------------------------------------------------

#include <iostream>
#include <X11/KBlib.h>
#include <X11/Xlib.h>

using namespace std;

int main( int argc, char *argv[] ){

unsigned kbd_state, led_state=0;
XkbGetIndicatorState( NULL, XkbUseCoreKbd, &kbd_state );

// here is the logic for lock detection
if( (kbd_state & 0x01) == 1 ) led_state += 1; // if caps lock is on
if( (kbd_state & 0x02) == 2 ) led_state += 2; // if number lock is on
if( (kbd_state & 0x04) == 4 ) led_state += 4; // if scroll lock is on
// if( (kbd_state & ????) == ? ) led_state += ?; // is kana lock on??????

return 0;

}

//---------------------------------------------------


Any help would be really appreciated! Thanks!

high_flyer
13th September 2011, 10:36
Qt::Key_Kana_Lock ?
Why not use the Qt enums for all the other keys as well?
It will increase the portability of your code.

dswartz
13th September 2011, 22:24
Seems to me that this key code is only useful for listening to see if the key is pressed or setting and unsetting its state. I need a way to determine whether or not it has already been pressed, so to speak. My application needs to know if caps lock is on or off, as well as for scroll/num/kana. Is there a function that will query the state of these locks in Qt?

high_flyer
14th September 2011, 10:03
Well, you could use QApplication::keyboardModifiers (), but I see in the docs that the Kana lock is not part of the information it returns.
What you can do however, is create an event filter, and filter the keyboard events, and set a flag each time the Kana_lock is pressed or released, so that you can know at later time its state.
I also would experiment to see, if any of the modifiers are returned (such as Qt::AltModifier or Qt::MetaModifier) when the Kana_lock is on, might save you a bit of "if()"'s.