Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How do I get key pressed using event filtering on Android?
Forum Updated to NodeBB v4.3 + New Features

How do I get key pressed using event filtering on Android?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
1 Posts 1 Posters 627 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nulik
    wrote on last edited by
    #1

    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
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved