I'm implementing a customized virtual keyboard using QML. My purpose is simulating a physical key press signal of real keyboard when I click a button in virtual keyboard. I have followed the tutorial in Qt Virtual Keyboard and have successfully built and run the example code.
The problem is that the example code employs QCoreApplication::sendEvent()
function in a C++ class to send key press event to focused QObject. It works well when I import QtQuick.Controls 1.3 in main.qml as the guide, but takes no effect when I change to QtQuick.Controls 2.2, which is essential in my application. Here is the core of the example code:
void KeyEmitter::emitKey(Qt::Key key)
{
QQuickItem* receiver = qobject_cast<QQuickItem*>(QGuiApplication::focusObject());
if(!receiver) {
return;
}
QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString());
QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier);
QCoreApplication::sendEvent(receiver, &pressEvent);
QCoreApplication::sendEvent(receiver, &releaseEvent);
}
So how can I send the key press event to my application?