Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Intercept QML keys on C++ side.

Intercept QML keys on C++ side.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
24 Posts 2 Posters 7.5k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    egor.utsov
    wrote on last edited by
    #1

    Hi all. Is it possible to intercept keyboard events on C++ side with UI in QML? I've already tried to install event filter on whole QApplication::instance() and on QQMLEngine particulary, it didn't help to catch keyboard events.

    My main.cpp looks like:

    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        QQmlApplicationEngine engine;
        engine.installEventFilter(new KeysInterceptor());
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();
    }
    

    Interceptor:

    class KeysInterceptor : public QObject
    {
        Q_OBJECT
    public:
        explicit KeysInterceptor(QObject *parent = nullptr);
    
    
        // QObject interface
    public:
        virtual bool event(QEvent *event) override;
        virtual bool eventFilter(QObject *watched, QEvent *event) override;
    };
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You can do it. You can install filter on the app itself & try to do something. You can do is one part. I'm wondering any reason to get into this hack ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • E Offline
        E Offline
        egor.utsov
        wrote on last edited by egor.utsov
        #3

        @dheerendra As i said, i tried already to install event filter for whole application, but QKeyEvents not appear. I need to black the screen by timer, that count after last key was pressed. In other words - if device is idle for some time, screen should be shutdown.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          How do you find that device is idle ? Can you explain ? Based on this there can be another solution.
          Coming to filters -
          We can make it work. Show me your code inside the eventFilter. Did you install the filter for QApplication object ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • E Offline
            E Offline
            egor.utsov
            wrote on last edited by
            #5

            @dheerendra Every key press should reset timer inside filter object. Once timer fired - signal generated and device black out screen.

            Interceptor:

            bool KeysInterceptor::event(QEvent *event)
            {
                qDebug() << __FUNCTION__ << ":" << event->type();
                return QObject::event(event);
            }
            
            
            bool KeysInterceptor::eventFilter(QObject *watched, QEvent *event)
            {
                qDebug() << __FUNCTION__ << ":" << watched << " - " << event->type();
                return QObject::eventFilter(watched, event);
            }
            
            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              with this code for which object you have installed the filter ? Is it for engine or app object ?

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • E Offline
                E Offline
                egor.utsov
                wrote on last edited by
                #7

                @dheerendra I tried both ways below:

                QApplication::instance()->installEventFilter(new KeysInterceptor);
                
                QQmlApplicationEngine engine;
                engine.installEventFilter(new KeysInterceptor);
                
                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  @egor.utsov said in Intercept QML keys on C++ side.:

                  app

                  Just try..

                  KeysInterceptor *keyI = new KeysInterceptor;
                  app.installEventFilter(keyI);
                  

                  Remove event(...) method.

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    egor.utsov
                    wrote on last edited by
                    #9

                    @dheerendra
                    main.cpp:

                    QApplication app(argc, argv);
                    KeysInterceptor *ki = new KeysInterceptor;
                    app.installEventFilter(ki);
                    

                    KeyInterceptor.cpp:

                    bool KeysInterceptor::eventFilter(QObject *watched, QEvent *event)
                    {
                        if (event->type() == QEvent::Type::KeyPress || event->type() == QEvent::KeyRelease)
                            qDebug() << __FUNCTION__ << ":" << watched << " - " << event->type();
                    
                        return QObject::eventFilter(watched, event);
                    }
                    
                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      egor.utsov
                      wrote on last edited by
                      #10

                      @dheerendra i just checked it on desktop and it works like a charm

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by dheerendra
                        #11

                        This should work. What is the issue ? Sorry i missed seeing your response.
                        Question - To blackout your screen are you dependent on the key events ?

                        Dheerendra
                        @Community Service
                        Certified Qt Specialist
                        http://www.pthinks.com

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          egor.utsov
                          wrote on last edited by
                          #12

                          @dheerendra yes. Blackout should happen if no activity on keyboard is observed for ten minutes for example

                          1 Reply Last reply
                          0
                          • dheerendraD Offline
                            dheerendraD Offline
                            dheerendra
                            Qt Champions 2022
                            wrote on last edited by dheerendra
                            #13

                            You can do this with simple timer which fires after 10 minutes. Inside the handler just check whether key event happened. I'm not in favor of event filter in QApplication becz, every event goes there, checks & then responds. It is risky & performance hit.

                            Dheerendra
                            @Community Service
                            Certified Qt Specialist
                            http://www.pthinks.com

                            1 Reply Last reply
                            1
                            • E Offline
                              E Offline
                              egor.utsov
                              wrote on last edited by
                              #14

                              @dheerendra how i can save time of last key event in this case?

                              1 Reply Last reply
                              0
                              • dheerendraD Offline
                                dheerendraD Offline
                                dheerendra
                                Qt Champions 2022
                                wrote on last edited by dheerendra
                                #15

                                you can use the following

                                Window {
                                    visible: true
                                    width: 640
                                    height: 480
                                    title: qsTr("Hello World")
                                    property date startDate :new Date();
                                    property int mils;
                                    Timer{
                                        id : tim
                                        interval: 10000
                                        repeat: true
                                        running: true
                                        onTriggered: {
                                            var end  = new Date();
                                            var elapsed = end.getTime() - startDate.getTime()
                                            if (elapsed>=10000){
                                                console.log("Blackout")
                                            }else {
                                                console.log("No blackout")
                                                startDate = new Date()
                                            }
                                        }
                                    }
                                
                                    Rectangle{
                                        anchors.fill: parent
                                        focus: true
                                        objectName: "Dheerendra"
                                        Keys.onPressed: {
                                            console.log("Key is pressed")
                                            startDate = new Date();
                                        }
                                    }
                                }
                                

                                Dheerendra
                                @Community Service
                                Certified Qt Specialist
                                http://www.pthinks.com

                                1 Reply Last reply
                                1
                                • E Offline
                                  E Offline
                                  egor.utsov
                                  wrote on last edited by
                                  #16

                                  @dheerendra i have many places, where key processed. It is hard to put such code everywhere

                                  1 Reply Last reply
                                  0
                                  • dheerendraD Offline
                                    dheerendraD Offline
                                    dheerendra
                                    Qt Champions 2022
                                    wrote on last edited by
                                    #17

                                    It can be componetized as well. If it is everywhere, catch at the source only with qApp.

                                    Dheerendra
                                    @Community Service
                                    Certified Qt Specialist
                                    http://www.pthinks.com

                                    1 Reply Last reply
                                    1
                                    • E Offline
                                      E Offline
                                      egor.utsov
                                      wrote on last edited by
                                      #18

                                      @dheerendra once again - key events does not appear in filter. Think it is worth to mention that i use linuxfb plugin withOUT libinput.

                                      1 Reply Last reply
                                      0
                                      • dheerendraD Offline
                                        dheerendraD Offline
                                        dheerendra
                                        Qt Champions 2022
                                        wrote on last edited by
                                        #19

                                        basic question - In your program are you able to catch/handle the keyboard events ?

                                        Dheerendra
                                        @Community Service
                                        Certified Qt Specialist
                                        http://www.pthinks.com

                                        1 Reply Last reply
                                        1
                                        • E Offline
                                          E Offline
                                          egor.utsov
                                          wrote on last edited by
                                          #20

                                          @dheerendra qml ui react to key presses.

                                          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