hi all!
i'll tell you how to determine the state of CapsLock (this code can be modifyed for NumLock/ScrollLock, but you are to do that by yourself)
note: this will work under windows and any system that uses X11.
the function is simple:
bool QMyClass::checkCapsLock()
{
// platform dependent method of determining if CAPS LOCK is on
#ifdef Q_OS_WIN32 // MS Windows version
return GetKeyState(VK_CAPITAL) == 1;
#else // X11 version (Linux/Unix/Mac OS X/etc...)
Display * d = XOpenDisplay((char*)0);
bool caps_state = false;
if (d)
{
unsigned n;
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
caps_state = (n & 0x01) == 1;
}
return caps_state;
#endif
}
bool QMyClass::checkCapsLock()
{
// platform dependent method of determining if CAPS LOCK is on
#ifdef Q_OS_WIN32 // MS Windows version
return GetKeyState(VK_CAPITAL) == 1;
#else // X11 version (Linux/Unix/Mac OS X/etc...)
Display * d = XOpenDisplay((char*)0);
bool caps_state = false;
if (d)
{
unsigned n;
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
caps_state = (n & 0x01) == 1;
}
return caps_state;
#endif
}
To copy to clipboard, switch view to plain text mode
but we must have some includes:
#ifdef Q_OS_WIN32
# include <windows.h>
#else
# include <X11/XKBlib.h>
#endif
#ifdef Q_OS_WIN32
# include <windows.h>
#else
# include <X11/XKBlib.h>
#endif
To copy to clipboard, switch view to plain text mode
to use X11/XKBLib.h, you have to install libx11-dev and link with -lX11
but there are some troubles!
for example, let's see eventFilter:
{
if (event
->type
() == QEvent::KeyPress) { // ...
}
}
bool QMyClass::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::KeyPress) {
// ...
}
}
To copy to clipboard, switch view to plain text mode
look's good, but won't work under X11:
error: expected token ':' got '2'
error: expected token ':' got '2'
To copy to clipboard, switch view to plain text mode
why? what happened?
everything is simple. just look at X11/X.h:
[186] #define KeyPress 2
[186] #define KeyPress 2
To copy to clipboard, switch view to plain text mode
so that's that! we need to #undef those defines that conflicts with QEvent::Type enum:
#ifdef Q_OS_WIN32
# include <windows.h>
#else
# include <X11/XKBlib.h>
# undef KeyPress
# undef KeyRelease
# undef FocusIn
# undef FocusOut
// etc...
#endif
#ifdef Q_OS_WIN32
# include <windows.h>
#else
# include <X11/XKBlib.h>
# undef KeyPress
# undef KeyRelease
# undef FocusIn
# undef FocusOut
// etc...
#endif
To copy to clipboard, switch view to plain text mode
this will make some troubles if you want to use some specific X11 features, but i think if you are using Qt, you don't need that ;]
that's all, see ya =)
PS: sorry for my bad english
Bookmarks