I'm trying to grab a key to use it as a global shortcut in X11. I reduced my code to a simple test:

Qt Code:
  1. #include <QtGui>
  2. #include <QX11Info>
  3.  
  4. #include <X11/X.h>
  5. #include <X11/Xlib.h>
  6. #include <X11/keysym.h>
  7.  
  8. #ifdef KeyPress
  9. const int XKeyPress = KeyPress;
  10. const int XKeyRelease = KeyRelease;
  11. #undef KeyPress
  12. #undef KeyRelease
  13. #endif
  14.  
  15. class GrabWidget : public QTextBrowser
  16. {
  17. Q_OBJECT
  18.  
  19. public:
  20. GrabWidget(QWidget *parent)
  21. : QTextBrowser(parent)
  22. {
  23. qApp->installEventFilter(this);
  24.  
  25. m_keyCode = XKeysymToKeycode(QX11Info::display(), XK_F11);
  26. XGrabKey(QX11Info::display(), m_keyCode, ControlMask|ShiftMask, QX11Info::appRootWindow(), False, GrabModeAsync, GrabModeAsync);
  27. XFlush(QX11Info::display());
  28. }
  29.  
  30. ~GrabWidget()
  31. {
  32. XUngrabKey(QX11Info::display(), m_keyCode, ControlMask|ShiftMask, QX11Info::appRootWindow());
  33. }
  34.  
  35. bool GrabWidget::eventFilter(QObject */*watched*/, QEvent *event)
  36. {
  37. if(event->type() == QEvent::KeyPress) {
  38. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
  39. int qtKey = keyEvent->key();
  40. if(keyEvent->modifiers() == (Qt::ControlModifier|Qt::ShiftModifier) && qtKey == Qt::Key_F11) {
  41. insertPlainText("Ctrl+Shift+F11 pressed!\n");
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47.  
  48. private:
  49. int m_keyCode;
  50. };
To copy to clipboard, switch view to plain text mode 

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.