Results 1 to 4 of 4

Thread: 'Modifier' key notification (eg. Shift, Ctrl, Alt)

  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 'Modifier' key notification (eg. Shift, Ctrl, Alt)

    Is there a way of getting an event or slot called when a modifier key such as Shift, Ctrl, or Alt has been pressed regardless of what widget currently has the focus?

    So the user presses the left Shift button and an event handler gets called to tell the app that the shift button has been pressed. It doesn't matter if it can't differentiate between left shift and right shift.

    It is important that the widget doesn't need focus in order to get the event.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Modifier' key notification (eg. Shift, Ctrl, Alt)

    Hi there!

    I guess you have thought of eventFilters?

    Qt Code:
    1. class Widget : public QWidget
    2. { Q_OBJECT
    3. public:
    4. Widget(QWidget* parent = 0) : QWidget(parent)
    5. {
    6. QLineEdit* a = new QLineEdit();
    7. a->installEventFilter(this);
    8. l->addWidget(a);
    9. QLineEdit* b = new QLineEdit();
    10. b->installEventFilter(this);
    11. l->addWidget(b);
    12. QLineEdit* c = new QLineEdit();
    13. c->installEventFilter(this);
    14. l->addWidget(c);
    15. statusL = new QLabel();
    16. l->addWidget(statusL);
    17. setLayout(l);
    18. }
    19. protected slots:
    20. void resetStatus()
    21. {
    22. statusL->setText("");
    23. }
    24. protected:
    25. bool eventFilter(QObject *obj, QEvent *event)
    26. {
    27. if (event->type() == QEvent::KeyPress) {
    28. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    29. if (keyEvent->key() == Qt::Key_Shift)
    30. {
    31. statusL->setText("Shift pressed");
    32. }
    33. if (keyEvent->key() == Qt::Key_Control)
    34. {
    35. statusL->setText("Ctrl pressed");
    36. }
    37. QTimer::singleShot(500, this, SLOT(resetStatus()));
    38. }
    39. // standard event processing
    40. return QObject::eventFilter(obj, event);
    41. }
    42. private:
    43. QLabel* statusL;
    44. };
    To copy to clipboard, switch view to plain text mode 
    HIH

    Johannes

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Modifier' key notification (eg. Shift, Ctrl, Alt)

    If you want it application wide, why not install an event filter for your app:

    Qt Code:
    1. QApplication a(argc, argv);
    2. Widget* w = new Widget();
    3. a.installEventFilter(w);
    4. w->show();
    5. return a.exec();
    To copy to clipboard, switch view to plain text mode 
    Thats saves you the installation of the event filter for each widget.

    Johannes

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 'Modifier' key notification (eg. Shift, Ctrl, Alt)

    I didn't want to install an application-level event filter as I didn't want to slow down the main message loop, but it seemed that was the only way, so I redesigned the way everything worked and used another method (only pickup the modifier key notification from just certain main widgets).

Similar Threads

  1. Shortcut: CTRL + SHIFT + "letters"
    By smarinr in forum Qt Programming
    Replies: 17
    Last Post: 22nd May 2009, 09:34
  2. Replies: 1
    Last Post: 8th March 2007, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.