PDA

View Full Version : KeyPress and KeyRelease Events



sattu
30th September 2011, 08:40
Hi Everyone,

To see how various keys behave in the context of Qt when pressed and released, I added 2 VIRTUAL FUNCTIONS to my MainWindow Class and implemented them in the following way:


void MainWindow::keyPressEvent ( QKeyEvent * event )
{
qDebug("Came here,,,, Key Pressed");
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
qDebug("Came here,,,, Key Released");
}


What I observed is that, whenever I pressed any key, I would get both the qDebug statements as expected. But in the case of following four keys, I get only the qDebug of the keyReleaseEvent method:

Tab key, LeftArrow key, RightArrow key, SpaceBar

That means, for these keys only release event happens. I am really confused :confused:. Can anyone please explain why it is so?


With Regards,
Sattu

mentalmushroom
30th September 2011, 10:42
I have both events for those keys (Qt 4.7.2 + windows 7). Here is my code:



#include <QApplication>
#include <QtGui>

class MyWindow: public QMainWindow
{
protected:
virtual void keyPressEvent(QKeyEvent * event);
virtual void keyReleaseEvent(QKeyEvent * event);
};

void MyWindow::keyPressEvent(QKeyEvent *)
{
qDebug() << "key pressed";
}

void MyWindow::keyReleaseEvent(QKeyEvent *)
{
qDebug() << "key released";
}

int main(int argc, char * argv[])
{
QApplication a(argc, argv);
MyWindow w;
w.show();
return a.exec();
}


make sure you have your widget focused.

sattu
30th September 2011, 11:14
void MyWindow::keyPressEvent(QKeyEvent *)
{
qDebug() << "key pressed";
}

void MyWindow::keyReleaseEvent(QKeyEvent *)
{
qDebug() << "key released";
}

int main(int argc, char * argv[])
{
QApplication a(argc, argv);
MyWindow w;
w.show();
return a.exec();
}


make sure you have your widget focused.

Hi Mushroom,
Do one thing. Put 2 PushButtons on your Mainwindow.ui and see if you get the same result for the 4 keys I had mentioned.

mentalmushroom
30th September 2011, 11:51
well, if you add controls to your window, keys like tab, arrow or space will be used to change focus or press the button etc, so these events are processed by the window children.

sattu
30th September 2011, 12:29
well, if you add controls to your window, keys like tab, arrow or space will be used to change focus or press the button etc, so these events are processed by the window children.

Well, I understood you partially. I mean, if I add any controls, I get the KeyRelease Event for the keys I mentioned, but not KeyPress. How come? Can you explain a bit more?

mentalmushroom
30th September 2011, 14:45
i think, only key press events for those keys have the special meaning, so this is why only key press is "eaten". you may notice you change the focus when you press tab, not when you release it. if you wish to process those events installEventFilter may probably help in that.

sattu
30th September 2011, 15:08
i think, only key press events for those keys have the special meaning, so this is why only key press is "eaten". you may notice you change the focus when you press tab, not when you release it. if you wish to process those events installEventFilter may probably help in that.

Yes, I do want to process those events, But I have never been through "installEventFilter" concept. So, it's a normal pre-defined method or something like that?
Give me a minimum idea so that I immediately start with it.

mentalmushroom
30th September 2011, 15:17
I'd suggest you to read the documentation for installEventFilter function. I think, it explains much better than I can do.

sattu
30th September 2011, 19:23
Ok Thanks, Will go through the documentation.