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.
Qt 6.11 is out! See what's new in the release blog

Intercept QML keys on C++ side.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
24 Posts 2 Posters 14.0k 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
    #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
        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
        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
        https://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
            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
            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
            https://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
                Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                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
                https://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
                    Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                    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
                    https://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
                        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                        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
                        https://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
                          • dheerendraD Offline
                            dheerendraD Offline
                            dheerendra
                            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                            wrote on last edited by
                            #21

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

                            if (event->type() == QEvent::Type::KeyPress || event->type() == QEvent::KeyRelease)

                            Can you just print the event type in eventFilter function ? Right now it is condition based. When you press the keyboard let us see what type events are coming. This may give us hint. If not we will do something else.

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

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

                              @dheerendra i am also tried to enable QT_LOGGING_RULES="qt.qpa.input=true", but key events still not printed, however i see beginning of qpa logs

                              1 Reply Last reply
                              0
                              • dheerendraD Offline
                                dheerendraD Offline
                                dheerendra
                                Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                                wrote on last edited by dheerendra
                                #23

                                while we debug this issue, another option what you can try is Keys.forwardTo option in qml. We can make all the key events forward to one common object. This common object can handle your blackout logic.

                                what is happening in eventfilter function ? Is it printing the type as key event or not ?

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

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

                                  @dheerendra Hi. I reboot the unit and now key events appeared in filter. I don't know whats changed, but it works now. Thank you for help and sorry for bothering

                                  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