PDA

View Full Version : Key Press Event and Shift key problem



Teefs
25th February 2011, 21:32
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:

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;
}
}

JohannesMunk
25th February 2011, 21:57
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.



main.h:

#ifndef MAIN_H
#define MAIN_H

#include <QtGui>

class MainWindow : public QMainWindow
{
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[])
{
QApplication a(argc, argv);

MainWindow mw;
mw.show();

return a.exec();
}


I'm on Win7x64 Qt 4.71 Mingw.

Joh

Teefs
25th February 2011, 22:34
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.

JohannesMunk
25th February 2011, 23:09
I checked it in a Kubuntu 10.10 Virtual Machine with Qt 4.70. Works as it should.

Joh

Teefs
26th February 2011, 00:57
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?

JohannesMunk
26th February 2011, 08:09
VirtualBox 4.0.

You are welcome!

Joh

Rishi
10th March 2011, 19:34
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

dirkdepauw
18th April 2011, 09:39
To fix this problem when running a Parallels VM: Preferences > Keyboard > Disable 'Use Ctrl+Shift + click to right-click'