Key Press Event and Shift key problem
I'm having trouble getting key press events to work properly with the Shift key (it looks like Ctrl and Alt have this problem too, but most other keys work as expected). I'm overriding the keyPressEvent() method to try to detect when the Shift key is pressed. However, whenever i press the shift key i don't receive the event immediately. I only receive the event when i release the shift key or press another key on the keyboard.
The event for the shift key always fires immediately before the second event. Instead I want it to fire the instant I start holding down the shift key.
Does anyone know how to make this work, or perhaps a workaround that gives similar functionality?
Here's a snippet of the code i'm using:
Code:
void MainWindow
::keyPressEvent ( QKeyEvent *event
) { using namespace Qt;
if ( event->isAutoRepeat() ) {
event->ignore();
return;
}
switch ( event->key() ) {
//...
//...
//...
case Key_Shift:
std::cout << "SHIFT PRESS" << std::endl;
break;
default:
event->ignore();
return;
}
}
Re: Key Press Event and Shift key problem
Hi!
I just tried this. Works fine here. The Shift-Key-Press event is sent immediately - regardless whether I hold it down to press another key or release it immediately.
Code:
main.h:
#ifndef MAIN_H
#define MAIN_H
#include <QtGui>
{
protected:
virtual void keyPressEvent
( QKeyEvent * event
) {
qDebug() << event->modifiers() << event->key() << (event->key() == Qt::Key_Shift) << event->isAutoRepeat();
}
};
#endif // MAIN_H
main.cpp
#include <QtCore>
#include <QtGui>
#include "main.h"
int main(int argc, char *argv[])
{
MainWindow mw;
mw.show();
return a.exec();
}
I'm on Win7x64 Qt 4.71 Mingw.
Joh
Re: Key Press Event and Shift key problem
I compiled an ran your code and it exhibits the same problem.
I'm using Ubuntu 10.04.1 32bit, so perhaps it is a bug with the OS or the libraries in the Ubuntu repository.
Re: Key Press Event and Shift key problem
I checked it in a Kubuntu 10.10 Virtual Machine with Qt 4.70. Works as it should.
Joh
Re: Key Press Event and Shift key problem
I forgot to mention earlier that I am running it in a VM, and it looks that that is my issue after all. It looks like it is a bug with Parallels 5.0.
Thanks for all your help.
By the way, what VM software are you using that works properly?
Re: Key Press Event and Shift key problem
VirtualBox 4.0.
You are welcome!
Joh
Re: Key Press Event and Shift key problem
Hi Guys,
I'm new to QT and have been facing a similar problem with QT 3.3 on an MVL distribution with kernel 2.6.32.20.
After a fresh system boot up, I don't get the correct keyState in the keyboard events (QKeyEvent) i.e even when I've pressed the Alt key the event->state() returns zero. But, after i quit my application and restart it, I start getting the expected output.
Has anyone else faced a similar issue or can point me to the right place to look.
Thanks,
Rishi
Re: Key Press Event and Shift key problem
To fix this problem when running a Parallels VM: Preferences > Keyboard > Disable 'Use Ctrl+Shift + click to right-click'