Results 1 to 4 of 4

Thread: Control buttons with keyboard keys

  1. #1
    Join Date
    May 2013
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Control buttons with keyboard keys

    I made a small app that sends commands to a robot like forward, backward etc. I have four buttons and their clicked(); slot does what I want them to do but now I would like to use the keyboard up down left right to essentially push those buttons. What do I have to do to get the input from the keyboard to activate the slots?

  2. #2
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Control buttons with keyboard keys

    If this is a QWidget app, add something like the following to your class declaration:
    Qt Code:
    1. protected:
    2. void keyPressEvent(QKeyEvent *event);
    To copy to clipboard, switch view to plain text mode 

    Then something like this to the implementation of keyPressEvent:
    Qt Code:
    1. switch(event->key()){
    2. case Qt::Key_Up:
    3. do stuff for the up arrow
    4. break;
    5. case Qt::Key_Left:
    6. do stuff for the left arrow.
    7. break;
    8. case Qt::Key_Right:
    9. do stuff for the right arrow.
    10. break;
    11. case Qt::Key_Down:
    12. do stuff for the down arrow.
    13. break;
    14. case Qt::Key_0:
    15. break;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Karl

  3. #3
    Join Date
    May 2013
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Control buttons with keyboard keys

    Thanks Karl, I got it working with the W A S D keys but I cant seem to get the arrow keys working, any ideas as to why? Im using a laptop with ubuntu 13.04. I believe the focus has got something to do with it because I have a spin box in my app and when I hit the down key enough times it gets highlighted.

  4. #4
    Join Date
    May 2013
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Control buttons with keyboard keys

    anyone know?

Similar Threads

  1. Replies: 3
    Last Post: 11th December 2012, 20:48
  2. Assign shortcut keys to buttons
    By mecrazycoder in forum Newbie
    Replies: 0
    Last Post: 7th January 2011, 22:42
  3. keyboard buttons don't show up on embedded system but do on desktop, any ideas?
    By technoViking in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 29th July 2010, 11:49
  4. Replies: 0
    Last Post: 16th December 2009, 09:45
  5. Replies: 1
    Last Post: 6th July 2009, 13:48

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.