victor.fernandez
11th August 2008, 08:17
I'm trying to grab a key to use it as a global shortcut in X11. I reduced my code to a simple test:
#include <QtGui>
#include <QX11Info>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#ifdef KeyPress
const int XKeyPress = KeyPress;
const int XKeyRelease = KeyRelease;
#undef KeyPress
#undef KeyRelease
#endif
class GrabWidget : public QTextBrowser
{
Q_OBJECT
public:
GrabWidget(QWidget *parent)
: QTextBrowser(parent)
{
qApp->installEventFilter(this);
m_keyCode = XKeysymToKeycode(QX11Info::display(), XK_F11);
XGrabKey(QX11Info::display(), m_keyCode, ControlMask|ShiftMask, QX11Info::appRootWindow(), False, GrabModeAsync, GrabModeAsync);
XFlush(QX11Info::display());
}
~GrabWidget()
{
XUngrabKey(QX11Info::display(), m_keyCode, ControlMask|ShiftMask, QX11Info::appRootWindow());
}
bool GrabWidget::eventFilter(QObject */*watched*/, QEvent *event)
{
if(event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
int qtKey = keyEvent->key();
if(keyEvent->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier) && qtKey == Qt::Key_F11) {
insertPlainText("Ctrl+Shift+F11 pressed!\n");
return false;
}
}
return true;
}
private:
int m_keyCode;
};
Grabbing the key seems to be working since I don't get any error message from Xlib (whereas I get BadAccess if I try to grab a different key which is already grabbed by another application) but I don't get any key press events, nor even if I press that key combination when my own widget has focus. So, what's wrong? How should I receive KeyPress events when I grab a key? I also tried with x11Event() but I get events only when my widget has focus not when another has it.
Thanks.
PS: I know it's not portable but I already made an implementation for Windows using RegisterHotKey() and now I'm just trying to implement it for X11.
#include <QtGui>
#include <QX11Info>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#ifdef KeyPress
const int XKeyPress = KeyPress;
const int XKeyRelease = KeyRelease;
#undef KeyPress
#undef KeyRelease
#endif
class GrabWidget : public QTextBrowser
{
Q_OBJECT
public:
GrabWidget(QWidget *parent)
: QTextBrowser(parent)
{
qApp->installEventFilter(this);
m_keyCode = XKeysymToKeycode(QX11Info::display(), XK_F11);
XGrabKey(QX11Info::display(), m_keyCode, ControlMask|ShiftMask, QX11Info::appRootWindow(), False, GrabModeAsync, GrabModeAsync);
XFlush(QX11Info::display());
}
~GrabWidget()
{
XUngrabKey(QX11Info::display(), m_keyCode, ControlMask|ShiftMask, QX11Info::appRootWindow());
}
bool GrabWidget::eventFilter(QObject */*watched*/, QEvent *event)
{
if(event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
int qtKey = keyEvent->key();
if(keyEvent->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier) && qtKey == Qt::Key_F11) {
insertPlainText("Ctrl+Shift+F11 pressed!\n");
return false;
}
}
return true;
}
private:
int m_keyCode;
};
Grabbing the key seems to be working since I don't get any error message from Xlib (whereas I get BadAccess if I try to grab a different key which is already grabbed by another application) but I don't get any key press events, nor even if I press that key combination when my own widget has focus. So, what's wrong? How should I receive KeyPress events when I grab a key? I also tried with x11Event() but I get events only when my widget has focus not when another has it.
Thanks.
PS: I know it's not portable but I already made an implementation for Windows using RegisterHotKey() and now I'm just trying to implement it for X11.