[SOLVED]How to simulate a key sequence?
-
Hi all,
I am trying to simulate a key combination like "Ctrl + Shift + M " in my application but nothing happens when i call the function that does the job.My function is as follows:
@
void XXXXXXXXX::hideAndSimulateKeyPress()
{mMainWindow->actionHideApplication->trigger();
//THIS IS MY CODE TO SIMULATE THE COMBINATION .MAY BE THERE IS SOMETHING WRONG...
QTest::keyClick(UBApplication::desktop(), Qt::Key_M,Qt::ControlModifier&Qt::ShiftModifier);}
@
Thanks for your time. -
do you write a unit test case or do you use QTest in your regular application?
Why don't you also create a QAction which you do trigger manually. Additionally you set a application wide shortcut on it in case the user triggers it with the key combination? -
Thanks raven-works for your answer.
[quote author="raven-worx" date="1371192068"]do you write a unit test case or do you use QTest in your regular application?[/quote]
I use it in my regular application on a temporary basis[quote author="raven-worx" date="1371192068"]
Why don't you also create a QAction which you do trigger manually. ?[/quote]I want the combination to be trigered (simulated) internally from my application(Design requirement).
But the QTest::keyClick should work right?Haven't figured out yet what i am doing wrong.
Thanks again for your time.
-
it should work, yes. But you shouldn't send it to the QDesktopWidget ;)
I don't know how and where you catch the key event ("Ctrl+Shift+M")...
If you catch it in a child widget and send it to your mainwindow the child widget will never receive the event.
As i said you can create a QAction with an application widget shortcut set, or you add an event filter on the QApplication instance and look for the keyevent there ... -
Sorry may be I am not explaining well my problem.Here is the situation:
.I have a (separate) application that pops us a menu when we do the combination (“Ctrl+Shift+M”) on the keyboard..I have an action in my application that does the following when triggered:
-->>hide the application so the desktop screen has focus(That is why I retrieved a pointer to the desktop in the first argument of keyClick).
-->>simulate the combination so we have the same effect as if the user had done the combination on the keyboard.
Is there a way in Qt to retrieve a pointer to the desktop?
Edit: It is not my application that handles the event.
I apologize if this is long and thanks for your time. -
well that's not possible, at least with Qt only.
You will need to post the event/message OS API to send the appropriate event to the current active application. -
Thanks ,Guess I ll keep looking for another solution then.
-
What is weird to me, is this:
@
QTest::keyClick(UBApplication::desktop(), Qt::Key_M,Qt::ControlModifier&Qt::ShiftModifier);
@Why do you use the & operator? I think you need the | operator instead. Using & will result in a 0 argument there.
-
[quote author="Andre" date="1371197813"]What is weird to me, is this:
Why do you use the & operator? I think you need the | operator instead. Using & will result in a 0 argument there.[/quote]
I wanted to follow the examples from the book I use as my Qt reference.I know these are some hex bitwise operations but I kind of was lazy to go dig more.
Any way I was able to solve my problem using some brute force but at least it works for now.The problem was finding a widget to pass as argument in the keyClick function.
Since the application that processes the event was not my Qt application that sent it ,I was told it was impossible to find that pointer.But I knew I have done something similar a few months ago on a Delphi application I was working on.So the idea came to me to write a small program to simulate the key combination (It doesn't require to pass that pointer) and start it using QProcess in my slot.
It works as I want but I want to know what more knowledgeable people think about this.And I hope it helps some one with the same issues.
Thanks guys.