keypressed and keyreleased
-
@genli
Yes,
in my idea in keyReleaseEvent you must clear keyPressed variable (for example keyPressed=""), so in the switch the program skips in default and you stop all moving.It is clear that this logic could be improved to avoid doing the same operations
Is not key pressed but moving manager ;)
-
-
I changed the code but it's the same error :
void Robot::keyPressEvent(QKeyEvent*keyevent)
{
if (keyevent->key() == Qt::Key_Z)
{ui->forward_btn->pressed();
}
}
void Robot::keyReleaseEvent(QKeyEvent*keyevent)
{if (keyevent->key() == Qt::Key_Z) { ui->forward_btn->released(); }
}
any idea please ? -
@genli
The idea is having one place where manage the moving.
So my idea is the same:- having a timer always actived
- defining a enum or defines for direction
- in key event or pushbutton event store last moving selection.
Only for example:
enum Moving
{
enStop,
enForward,
enBack,
enLeft,
enRight
}Moving moving;
void MyObject::core()
{
...
//Moving managerswitch( moving)
{
case enForward:
move1();
break;case enBack: move2(); break; ... default: stopMoving(); break;
}
...
}void MyObject:: keyReleaseEvent(QKeyEventkeyevent)
{
keyPressed = enStop;
}
void MyObject::keyPressEvent(QKeyEventkeyevent)
{
if ( keyEvent->key() == "S" )
keyPressed = enForward;
else if ( keyEvent->key() == "D" )
keyPressed = enBack;
...
}//for all 4 buttons write a code like this
void MyObject::on_forward_btn_pressed()
{
keyPressed = enForward;
]void MyObject::on_forward_btn_relesed()
{
keyPressed = enStop;
]The above code could have error, I write in editor and not in QTCreator.
For security the stopOperation I insert always in default.
-
@CP71 the thing I can't understand is when I press Z:
The debug console shows me:
Forward
keyReleased
Forward
keyReleased
Forward
keyReleased
and when I keep pressing z and I press another button for example R or P the problem solved
The debug console shows me:
Forward
Forward
Forward
how i can press a button on the keyboard using code ? -
@genli create QKeyEvent and post it to the event queue https://doc.qt.io/qt-5/qcoreapplication.html#postEvent
I think the above is overly complicated though. I'd go with a set of flags, either standalone or the vector of bool, and custom signal - let's say
keyboardStateChanged()
or something.
In keypressed/keyreleased events you just set the flag and emit the signal.
The signal it self connected as a queued connection, so it's just processed after the control returns from the keyboard event processing, connected to the slot which in turn, sends movement commands to the remote.
Less methods, less code the way I see it, clearer approach.EDIT: also, try do debug key events to see possible modifiers from the event. Those are platform dependent.
-
@genli
You have several ways to do this, depend on own style, as it often happens there isn't only one way to do something.There's a way to do it with signal and slot ?
Yes of course!
As @artwaw says you can use event filter.
I wouldn't use eventFiler, but not because is not correct, but only because it is not my style, doesn't mean that my style is the best way!The @artwaw's tip is good, in short, think simple, less code you have less bug you will have.
My tip is the same, having one point where you manage the moving. How you send the variable, flags, and so on depends only on you and how the project is designed.
Normally in my projects, I like to have an application core manager that is independent of UI. The UI and the core communicate between them via signals.
But before going ahead, for me, you must understand because you have:
Forward
keyReleased
when wouldn't be so.