Unsolved How do I get key pressed using event filtering on Android?
-
Hi all,
the problem is that on Android, I am not getting the key events when the User is typing keys in virtual keyboard. I get them when the User presses 'Done' button and the keyboard is closed. (they come all at once, btw) But to implement some interactive behavior I have to get the keys as the user types them. So I tried to register an event filter:
bool EventFilter::eventFilter(QObject *watched, QEvent *event ) { //qDebug() << "eventfilter: " << event->type() ; if (!watched) { return false; } if (!event) { return false; } if (watched->objectName() == "myedit") { qDebug() << "Field textedit edited: type="<< event->type(); if (event->type() == QEvent::InputMethod) { QInputMethodEvent *inputMethod = static_cast<QInputMethodEvent *>(event); qDebug() << "inputMethod received"; if (!inputMethod) { qDebug() << "Unable to cast correct type"; } else { QString commit = inputMethod->commitString(); qDebug() << "commit str:" << commit; QList<QInputMethodEvent::Attribute> list= inputMethod->attributes(); qDebug() << "attributes:"; for (int i=0;i<list.size();i++) { QInputMethodEvent::Attribute attr=list.at(i); QVariant var=attr.value; qDebug() << i << ": type=" << attr.type << var.toUrl(); } } } } return false; }
So, now when I am typing keys in virtual keyboard I am getting these events:
MyApp: ../MyApp/eventfilter.cpp:18 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): Field textedit edited: type= QEvent::Type(InputMethodQuery) D MyApp: ../MyApp/eventfilter.cpp:18 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): Field textedit edited: type= QEvent::Type(InputMethod) D MyApp: ../MyApp/eventfilter.cpp:21 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): inputMethod received D MyApp: ../MyApp/eventfilter.cpp:26 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): commit str: "" D MyApp: ../MyApp/eventfilter.cpp:28 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): attributes: D MyApp: ../MyApp/eventfilter.cpp:32 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): 0 : type= 1 QUrl("") D MyApp: ../MyApp/eventfilter.cpp:32 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): 1 : type= 0 QUrl("") D MyApp: ../MyApp/eventfilter.cpp:18 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): Field textedit edited: type= QEvent::Type(InputMethodQuery) D MyApp: ../MyApp/eventfilter.cpp:18 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): Field textedit edited: type= QEvent::Type(InputMethodQuery) D MyApp: ../MyApp/eventfilter.cpp:18 (virtual bool EventFilter::eventFilter(QObject*, QEvent*)): Field textedit edited: type= QEvent::Type(InputMethodQuery)
Unfortunately this method didn't get me expected results, because I was thinking the virtual keyboard would send QKeyEvent objects and I would easily get the key code, but it sends InputMethod and InputMethodQuery :(
QML code:
TextField { id: fld_field focus: true Layout.fillWidth: true placeholderText: "Field" property bool field_selected: false objectName: "myedit" inputMethodHints: Qt.ImhNoPredictiveText } Is there a way to get KeyEvent from `QInputMethodEvent` ? Or, is it possible to get the key codes as the user types them in the virtual keyboard somehow ? I am using Qt 5.11