[Solved] How to Handle all the keys in Key Press / Release Event?
-
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..
-
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() }