Results 1 to 3 of 3

Thread: Keyboard Capturing

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face Keyboard Capturing

    I am trying to have a QMainWindow capture keyPressEvents Qt::Key_Up and Qt::Key_Down. Unfortunately in my keyPressEvent( ) function, the print statements I have for these 2 keys do not print. My main window contains about 12 push buttons, 2 sliders, and 2 QGLWidgets. Upon further observation I noticed my 2nd slider bar was moving with the up and down keys.

    Is there a way to get my sliders and push buttons from accpeting the key presses so my main window can receive them? My keyPressEvent looks like this:


    Qt Code:
    1. void PlotWindow::keyPressEvent( QKeyEvent *evnt )
    2. {
    3. switch( evnt->key( ) )
    4. {
    5. case Qt::Key_Control:
    6. evnt->accept( );
    7. mpFirstPlot->setControlKeyPressed( true );
    8. break;
    9. case Qt::Key_Up:
    10. evnt->accept( );
    11. fprintf( stderr, "up pressed\n" );
    12. break;
    13. case Qt::Key_Down:
    14. evnt->accept( );
    15. fprintf( stderr, "down pressed\n" );
    16. break;
    17. default:
    18. evnt->ignore( );
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    Capturing the Control key works fine but I do not get anything to capture the up and down keys. Thanks for your help!

  2. #2
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keyboard Capturing

    Try subclassing the QSlider and reimplementing the keyPressEvent handler to ignore all incoming events.

    Qt Code:
    1. void NewSlider::keyPressEvent( QKeyEvent *evnt )
    2. {
    3. evnt->ignore( );
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Rayven for this useful post:

    ToddAtWSU (29th June 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Keyboard Capturing

    One option could be to install an event filter on the application object. This way you'll get the events before they reach any widget, though.
    J-P Nurmi

Similar Threads

  1. virtual keyboard done with qt4-designer
    By Klaus_EAN in forum Qt Programming
    Replies: 17
    Last Post: 30th December 2010, 08:47
  2. Virtual Keyboard on Qtopia 4.2.1
    By shapirlex in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 11th April 2007, 15:51
  3. Grab keyboard events in Windows
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2007, 19:56
  4. Replies: 2
    Last Post: 24th July 2006, 18:36
  5. Keyboard Handling
    By ToddAtWSU in forum Qt Programming
    Replies: 4
    Last Post: 5th July 2006, 13:25

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.