Results 1 to 10 of 10

Thread: keyboard - detecting key pressed

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: keyboard - detecting key pressed

    Quote Originally Posted by wysota View Post
    That's because the line edit consumes the event before it reaches the window object.
    I don't understand what you want but if you want to intercept key events before they reach their destinations, you need to use event filters.
    I've wrote my event filter, it looks like this:

    Qt Code:
    1. class KeyboardFilter : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. KeyboardFilter( QObject *parent = 0 ) : QObject( parent ) {}
    7.  
    8. signals:
    9. void up_arrow();
    10. void down_arrow();
    11. void left_arrow();
    12. void right_arrow();
    13.  
    14. protected:
    15. bool eventFilter( QObject *dist, QEvent *event )
    16. {
    17. if( event->type() == QEvent::KeyPress )
    18. {
    19. QKeyEvent *keyEvent = static_cast<QKeyEvent*>( event );
    20.  
    21. if( QString("1234567890").indexOf( keyEvent->text() ) == -1 ) return true;
    22. else if ( keyEvent->key() == Qt::Key_Up ) {emit up_arrow(); return true;}
    23. else if ( keyEvent->key() == Qt::Key_Down ) {emit down_arrow(); return true;}
    24. else if ( keyEvent->key() == Qt::Key_Left ) {emit left_arrow(); return true;}
    25. else if ( keyEvent->key() == Qt::Key_Right ) {emit right_arrow(); return true;}
    26. }
    27.  
    28. return false;
    29. }
    30. };
    To copy to clipboard, switch view to plain text mode 

    Almost everything works fine except Backspace key. How can I make It work in that text field on which that filter is installed?

    thanks in advance
    best regards
    Tomasz
    Last edited by Tomasz; 18th April 2011 at 23:44.

Similar Threads

  1. QTableView key pressed
    By radu_d in forum Qt Programming
    Replies: 6
    Last Post: 1st August 2013, 03:20
  2. Replies: 6
    Last Post: 4th October 2010, 03:19
  3. check the time of last time keyboard is pressed
    By Aki Tomar in forum General Programming
    Replies: 2
    Last Post: 5th February 2008, 09:10
  4. Getting the row for button pressed
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2007, 15:45
  5. Replies: 5
    Last Post: 1st March 2007, 08:19

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
  •  
Qt is a trademark of The Qt Company.