PDA

View Full Version : KeyPress and KeyRelease events not happening as they should



hypnotic401
24th January 2013, 18:53
Hello all,

I made a custom event filter for my application and installed it on my instance of QApplication. However, I noticed some strange behavior. When I press a key (once and it doesnt get held down) I do get the KeyPress event (I check for auto repeat first and if its false I handle the event other wise let event processing continue and this is because if someone holds down a key I do not want it sending out multiple keypress events and keyrelease events), I do not receive a KeyRelease event until I press the key again at which I do not receive the KeyPress. Any Ideas of what I could be doing wrong?

Below is my code that handles key events (I do some other checks above to make sure the widget in question or more specifcally the qobject in question has focus).
I pass emsg (which is an internal struct that I use to propagate events to other modules), I am using windows 7 64 bit but compiling and linking with Qt in 32 bit.

This is the use case of pressing a key once:


//q is pressed and immediately released
switch (event->type()) {

case QEvent::KeyPress:
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (!keyEvent->isAutoRepeat()) {
//It falls in here fine the first go around but never gets to released
emsg.wParam = keyEvent->key();
emsg.lParam = keyEvent->modifiers();
} else {
keyEvent->ignore();
return false;
}
break;
}
case QEvent::KeyRelease:
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (!keyEvent->isAutoRepeat()) {
//it never gets here but if I press q again it falls in here and not in keypress
emsg.wParam = keyEvent->key();
emsg.lParam = keyEvent->modifiers();
} else {
keyEvent->ignore();
return false;
}
break;
}
}

Santosh Reddy
25th January 2013, 12:59
How did you check release event / press event is not being received. I mean did you have a break point, or qDebug()....

Also are you trying to sniff the key, or eat(filter) the keys,In either cases return proper value (true/false).

hypnotic401
25th January 2013, 19:31
I had break points in each press and release. First time it was pressed i Received only the press and second time I pressed the same key I got the release event. My input manager returns true if it picked off the key event.

d_stranz
26th January 2013, 00:49
Using breakpoints to trace key and mouse events can be misleading because of the changes in focus and active window that occur when the breakpoint is hit. Try simply using qDebug() statements instead so the flow of execution isn't interrupted by breakpoints and see what gets reported.