I need to simulate Enter key event in Qt. How can I do this?
How can I simulate user interaction (key press event) in Qt?
The correct answer might be this:
QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QCoreApplication::postEvent (receiver, event);
In fact, there are no matching function for call to:
QtKeyEvent::QtKeyEvent(Type type, int key)
but there is:
QtKeyEvent::QtKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers)
Should be Qt::NoModifier not Qt::NoModifiers. –
Nightclub
It's important to simulate a KeyPress followed by a KeyReleased because there are objects that process something when the key is released. An example is a QWebView running a JavaScript that handles keyUp events. They won't be generated unless you post a KeyRelease event. –
Tien
You can also use
shareEvent
if you'd rather the QKeyEvent
be allocated on the stack. –
Cornelius QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter);
QCoreApplication::postEvent (receiver, event)
© 2022 - 2024 — McMap. All rights reserved.