PDA

View Full Version : Control buttons with keyboard keys



Robobulls
5th June 2013, 22:27
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?

KaptainKarl
5th June 2013, 22:54
If this is a QWidget app, add something like the following to your class declaration:

protected:
void keyPressEvent(QKeyEvent *event);

Then something like this to the implementation of keyPressEvent:

switch(event->key()){
case Qt::Key_Up:
do stuff for the up arrow
break;
case Qt::Key_Left:
do stuff for the left arrow.
break;
case Qt::Key_Right:
do stuff for the right arrow.
break;
case Qt::Key_Down:
do stuff for the down arrow.
break;
case Qt::Key_0:
break;
}

Karl

Robobulls
6th June 2013, 22:48
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.

Robobulls
11th June 2013, 22:55
anyone know?