Results 1 to 6 of 6

Thread: how to get key value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default how to get key value

    hi, i write a simple example, it function to print the key value
    this is the code:
    Qt Code:
    1. bool MainWindow::event(QEvent *event)
    2. {
    3. if (event->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    6.  
    7. qDebug()<<"kev value "<<keyEvent->key();
    8. }
    9.  
    10. return QMainWindow::event(event);
    11. }
    To copy to clipboard, switch view to plain text mode 

    but some time can not get some key value, for example end, up, down, page down key value can not print out.

    please tell me what why, and have a good example to get key value.
    thanks.

  2. #2
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to get key value

    i use another method to get the key value. the code it
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *k)
    2. {
    3. qDebug("in press event %x",k->key());
    4. }
    To copy to clipboard, switch view to plain text mode 
    but this method also can not get about NO. key and letter key.
    now, i don't know what the reason. please help.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to get key value

    You will not be able to get all the keys in QMainWindow, because QApplication may not be passing them on. To capture all the key events, you need install an event filter on the QApplication instance. Then you can have all the keys pressed, and then eat them, or pass them back.

    Example:

    Qt Code:
    1. class KeySniffer : public QObject
    2. {
    3. Q_OBJECT
    4. ...
    5. protected:
    6. bool eventFilter(QObject * obj, QEvent * event);
    7. };
    8.  
    9. bool KeySniffer::eventFilter(QObject * obj, QEvent * event)
    10. {
    11. if (event->type() == QEvent::KeyPress) {
    12. QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
    13. if(keyEvent)
    14. qDebug() << "Key pressed " << keyEvent->key() << keyEvent->text();
    15. }
    16.  
    17. // standard event processing
    18. return QObject::eventFilter(obj, event);
    19. }
    20.  
    21. MainWindow::MainWindow(QWidget *parent)
    22. : QMainWindow(parent)
    23. {
    24. ...
    25. // Filter installation on QApplication
    26. QApplication::instance()->installEventFilter(new KeySniffer(this));
    27. ...
    28. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to get key value

    Qt Code:
    1. bool KeyboardFilter::filter ( int unicode, int keycode, int modifiers, bool isPress, bool autoRepeat )
    To copy to clipboard, switch view to plain text mode 
    hi, what the unicode param fun.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to get key value

    Quote Originally Posted by lzpmail View Post
    Qt Code:
    1. bool KeyboardFilter::filter ( int unicode, int keycode, int modifiers, bool isPress, bool autoRepeat )
    To copy to clipboard, switch view to plain text mode 
    hi, what the unicode param fun.
    \
    Unicode univalent of the keycode

  6. #6
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to get key value

    ok, i know what should to do. thank you.

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.