Catching a key combination like(ALT+G) and Simulating the combination on a Button Push
-
Hi all,
I am trying to catch a key combination in my keypress event.The code I am trying is as follows.But as soon as I press the alt key the code in my if statement is directly executed.Thinking of adding a small delay to give the user some time to add other keys to the combination.Wondering if this is a good way to go about it.Also is there an easy way I can simulate the same key combination when a user presses a push button for example.I mean in a slot that responds to that button's clicked() signal.
Thanks for your time.@void Widget::keyPressEvent( QKeyEvent * event )
{
if(event->modifiers()&&Qt::AltModifier!=0)
{
//--->>DELAY IDEA HERE<<<----
//DO SOMETHING
}}@
-
Thanks qxoz
Your code is catching my combination.I also want to know if I am simulating the same combination the right way by the following code:
@void Widget::simulateGKey()
{
//QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifiers);
// QCoreApplication::postEvent (receiver, event);QTest::keyClick(this, Qt::Key_G,Qt::AltModifier);
}@
I know the code that is commented out is the best way to do this but I am only getting a key press and the event->text() code gives nothing.
Thanks for your time.
-
Try this:
@QKeyEvent key_press(QKeyEvent::KeyPress, Qt::Key_G, Qt::AltModifier, NULL, false, 0 );
QApplication::sendEvent(this, &key_press);QKeyEvent key_release(QKeyEvent::KeyRelease, Qt::Key_G, Qt::AltModifier, NULL, false, 0 );
QApplication::sendEvent(this, &key_release);@