Results 1 to 8 of 8

Thread: Key Press Event and Shift key problem

  1. #1
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. void MainWindow::keyPressEvent ( QKeyEvent *event ) {
    2. using namespace Qt;
    3.  
    4. if ( event->isAutoRepeat() ) {
    5. event->ignore();
    6. return;
    7. }
    8. switch ( event->key() ) {
    9. //...
    10. //...
    11. //...
    12.  
    13. case Key_Shift:
    14. std::cout << "SHIFT PRESS" << std::endl;
    15. break;
    16. default:
    17. event->ignore();
    18. return;
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. main.h:
    2.  
    3. #ifndef MAIN_H
    4. #define MAIN_H
    5.  
    6. #include <QtGui>
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. protected:
    11. virtual void keyPressEvent ( QKeyEvent * event )
    12. {
    13. qDebug() << event->modifiers() << event->key() << (event->key() == Qt::Key_Shift) << event->isAutoRepeat();
    14. }
    15.  
    16. };
    17.  
    18. #endif // MAIN_H
    19.  
    20.  
    21. main.cpp
    22.  
    23. #include <QtCore>
    24. #include <QtGui>
    25. #include "main.h"
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QApplication a(argc, argv);
    30.  
    31. MainWindow mw;
    32. mw.show();
    33.  
    34. return a.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

    I'm on Win7x64 Qt 4.71 Mingw.

    Joh

  3. #3
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Platforms
    Unix/X11

    Default 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.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  5. The following user says thank you to JohannesMunk for this useful post:

    Teefs (26th February 2011)

  6. #5
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Platforms
    Unix/X11

    Default 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?

  7. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Key Press Event and Shift key problem

    VirtualBox 4.0.

    You are welcome!

    Joh

  8. #7
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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

  9. #8
    Join Date
    Jan 2006
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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'

Similar Threads

  1. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 13:40
  2. QGraphicsview and mouse press event
    By eva2002 in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2010, 06:04
  3. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 11:28
  4. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 14:08
  5. Key Press Event trouble
    By morraine in forum Newbie
    Replies: 6
    Last Post: 18th August 2008, 09:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.