Results 1 to 10 of 10

Thread: keyboard - detecting key pressed

  1. #1
    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 keyboard - detecting key pressed

    Hello!

    I've got simple application in which I need to use 4 keyboard keys (arrows) to move point on my widget. I've got something like this in main window:

    Qt Code:
    1. void MainWindow::keyPressEvent( QKeyEvent *k )
    2. {
    3. switch ( k->key() )
    4. {
    5. case Qt::Key_Up:
    6. qDebug() << "UP";
    7. break;
    8. case Qt::Key_Down:
    9. qDebug() << "DOWN";
    10. break;
    11. case Qt::Key_Left:
    12. qDebug() << "LEFT";
    13. break;
    14. case Qt::Key_Right:
    15. qDebug() << "RIGHT";
    16. break;
    17. default:
    18. qDebug() << k->key() << endl;
    19. break;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    Inside I wanted to put function which changes coordinates of point inside widget. But only up/down keys works (prompt goes to first text edit window). How can I make it other way? I've read about qshortcuts but I don't know if I can use it (it can by shortcuts to real buttons on my application - then I'll make them).

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keyboard - detecting key pressed

    Could you explain what you mean by this sentence?
    Quote Originally Posted by Tomasz View Post
    But only up/down keys works (prompt goes to first text edit window).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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

    I've got two text QLineEdit fields. At start of application when I press UP or DOWN cursor/prompt goes to first field and stays there. Up/Down arrows works but when I press other keys (Left/Right or letters) I can't see qDebug() message - cursor stays in that field and It goes to right or left or writes letter which I pressed.
    I've got idea to use 4 QButton instead of keyboard but keyboard is more comfortable (maybe I should use 4 QButton and QShorcut? - but I don't know how to use it with that buttons or if It's even possible).

    thanks in advance
    best regards
    Tomasz

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keyboard - detecting key pressed

    Quote Originally Posted by Tomasz View Post
    Up/Down arrows works but when I press other keys (Left/Right or letters) I can't see qDebug() message - cursor stays in that field and It goes to right or left or writes letter which I pressed.
    That's because the line edit consumes the event before it reaches the window object.

    I've got idea to use 4 QButton instead of keyboard but keyboard is more comfortable (maybe I should use 4 QButton and QShorcut? - but I don't know how to use it with that buttons or if It's even possible).
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Tomasz (18th April 2011)

  6. #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.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keyboard - detecting key pressed

    Quote Originally Posted by Tomasz View Post
    How can I make It work in that text field on which that filter is installed?
    Try to avoid using words such as "it" and "that" if you're not sure it is clear what they refer to. I have no idea what you want. I would say line #28 of your snippet is wrong, though. You should probably call the base class implementation instead of returning false.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    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
    Try to avoid using words such as "it" and "that" if you're not sure it is clear what they refer to. I have no idea what you want. I would say line #28 of your snippet is wrong, though. You should probably call the base class implementation instead of returning false.
    I want my filter not response to backspace key (so clearing text in text field would be possible). I still don't get events thing very well. Probably You're right about base class implementation, but what it would be in that case? QLineEdit::keyPressEvent?

    thanks in advance
    best regards
    Tomasz

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keyboard - detecting key pressed

    Quote Originally Posted by Tomasz View Post
    I want my filter not response to backspace key (so clearing text in text field would be possible).
    Then don't return true from the event filter for the backspace key.

    Probably You're right about base class implementation, but what it would be in that case? QLineEdit::keyPressEvent?
    No, the base class implementation of the method you're currently implementing - eventFilter().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    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

    How should I call eventFilter in that case?

    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_Backspace ) eventFilter(dist, event);
    23. else if ( keyEvent->key() == Qt::Key_Up ) {emit up_arrow(); return true;}
    24. else if ( keyEvent->key() == Qt::Key_Down ) {emit down_arrow(); return true;}
    25. else if ( keyEvent->key() == Qt::Key_Left ) {emit left_arrow(); return true;}
    26. else if ( keyEvent->key() == Qt::Key_Right ) {emit right_arrow(); return true;}
    27.  
    28. }
    29.  
    30. return false;
    31.  
    32. }
    33. };
    To copy to clipboard, switch view to plain text mode 

    Should It be called for existing object (myLineEdit->eventFilter) or in different way?

    thanks in advance
    best regards
    Tomasz

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keyboard - detecting key pressed

    The convention is:
    Qt Code:
    1. ParentClassName::methodName(arguments);
    To copy to clipboard, switch view to plain text mode 

    Since you're not handling backspace yourself then you certainly shouldn't add an entry for it in your code. Just call the base class implementation instead of returning false although since your base class is QObject that doesn't do any filtering of its own return false should be fine too. I have no idea what you are trying to do but I have to admit your implementation is... odd. Especially the part where you consume the event if the text of the event doesn't contain a number. It is obvious none of the if blocks below will ever fire as none of the keys will generate a digit when pressed. I think you might be misinterpreting what QKeyEvent::text() does. Are you sure QIntValidator is not what you're after?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.