Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to Handle all the keys in Key Press / Release Event?
QtWS25 Last Chance

[Solved] How to Handle all the keys in Key Press / Release Event?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 68.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    I have a QLabel. In that i am going to handle the Key Press and the Key Release Events. For example. If i am Pressing a key (ex. key Q). the text of the Label should change to "You Pressed Key Q" like that. and if i Release it the Text should be changed to "You Released Key Q". I know how to handle it for a single particular key. Can anyone plz help me how can i do this for the all the keys?

    Thanks and Regards..

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Macro
      wrote on last edited by JKSH
      #2

      For a Single Particular key i can do like this,

      KeyPress.h

      #include <QWidget>
      #include <QtGui>
      
      class KeyPress : public QWidget
      {
          Q_OBJECT
      
      public:
          KeyPress(QWidget *parent = 0);
      
      protected:
          void keyPressEvent(QKeyEvent *event);
          void keyReleaseEvent(QKeyEvent *event);
      
      private:
          QLabel *myLabelText;
      };
      

      KeyPress.cpp

      #include <QApplication>
      #include <QKeyEvent>
      #include "KeyPress.h"
      
      KeyPress::KeyPress(QWidget *parent) :
          QWidget(parent)
      {
          myLabelText = new QLabel ("You Didn't Pressed / Released any Key");
      }
      
      void KeyPress::keyPressEvent(QKeyEvent *event)
      {
          if(event->key() == Qt::Key_Q)
          {
              myLabelText->setText("You Pressed Key Q");
          }
          else
          {
              myLabelText->setText("You Pressed Other Key");
          }
      }
      
      void KeyPress::keyReleaseEvent(QKeyEvent *event)
      {
          if(event->key() == Qt::Key_Q)
          {
              myLabelText->setText("You Released Key Q");
          }
          else
          {
              myLabelText->setText("You Released Other Key");
          }
      }
      

      main.cpp

      #include <QtGui/QApplication>
      #include "KeyPress.h"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          KeyPress *keyPress = new KeyPress();
          keyPress->show();
      
          return a.exec()
      }
      
      1 Reply Last reply
      1
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by JKSH
        #3

        What about ... ?

        void Widget::keyPressEvent(QKeyEvent *ev)
        {
            setText("You Pressed Key " + ev->text());
        }
        
        void Widget::keyReleaseEvent(QKeyEvent *ev)
        {
            setText("You Release Key " + ev->text());
        }
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          Macro
          wrote on last edited by
          #4

          Super Cool... It works fine. Thanks a lot... :)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved