Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to trigger a hotkey to a QWindow using a QButton?
Forum Updated to NodeBB v4.3 + New Features

How to trigger a hotkey to a QWindow using a QButton?

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 3 Posters 1.5k Views 2 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.
  • Pl45m4P Pl45m4

    @fabriciokash

    You can trigger QShortCut or QActions without typing the actual keys and send it to the destination widget.

    Or you can create a QKeyEvent and send to to your widget

    https://doc.qt.io/qt-5/qkeyevent.html

    fabriciokashF Offline
    fabriciokashF Offline
    fabriciokash
    wrote on last edited by
    #5

    @Pl45m4

    I have some really noob questions... Im trying to use QKeyEvent and what I need is to sendo a pair of keys, like ctrl+a, ctrl+v, etc. The question is about the constructor's parameters...

    I have here

    QKeyEvent *keyEvent = new QKeyEvent(QEvent::KeyPress, Qt::Key_Control, Qt::NoModifier)
    

    Whats difference between KeyPress and KeyRelease?

    And if the constructor accept only one Key, how I will use a pair of keys? like ctrl+A

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #6

      Hi,

      @fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:

      Whats difference between KeyPress and KeyRelease?

      The first when you press a key on the keyboard.
      The second when you release the key from the keyboard.

      @fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:

      And if the constructor accept only one Key, how I will use a pair of keys? like ctrl+A

      A is the key, Ctrl is the modifier.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      fabriciokashF 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        @fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:

        Whats difference between KeyPress and KeyRelease?

        The first when you press a key on the keyboard.
        The second when you release the key from the keyboard.

        @fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:

        And if the constructor accept only one Key, how I will use a pair of keys? like ctrl+A

        A is the key, Ctrl is the modifier.

        fabriciokashF Offline
        fabriciokashF Offline
        fabriciokash
        wrote on last edited by
        #7

        @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

        Hi,

        @fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:

        Whats difference between KeyPress and KeyRelease?

        The first when you press a key on the keyboard.
        The second when you release the key from the keyboard.

        @fabriciokash said in How to trigger a hotkey to a QWindow using a QButton?:

        And if the constructor accept only one Key, how I will use a pair of keys? like ctrl+A

        A is the key, Ctrl is the modifier.

        Aaah got it. I understood the key and modifier

        About press and release a key… it means i need two event objects? One for press and another for release to send to a widget?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #8

          Yes you do.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          fabriciokashF 1 Reply Last reply
          0
          • SGaistS SGaist

            Yes you do.

            fabriciokashF Offline
            fabriciokashF Offline
            fabriciokash
            wrote on last edited by
            #9

            @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

            Yes you do.

            I tried this code: two events inside a button clicked event

            void Simulate::on_selectAllBtn_clicked()
            {
                QKeyEvent *eventPress = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier);
                QKeyEvent *eventRealease = new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::ControlModifier);
                QCoreApplication::postEvent(targetWidget, eventPress);
                QCoreApplication::postEvent(targetWidget, eventRealease);
            }
            

            But I got a error on "KeyPress" and "KeyRelease": excepted unqualified-id before numeric constant

            Can you help me with that?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #10

              Can you show the compiler error message please ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              fabriciokashF 1 Reply Last reply
              0
              • SGaistS SGaist

                Can you show the compiler error message please ?

                fabriciokashF Offline
                fabriciokashF Offline
                fabriciokash
                wrote on last edited by fabriciokash
                #11

                @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

                Can you show the compiler error message please ?

                Yes, sure.

                Compile Output in Qt Creator

                Screenshot from 2021-11-05 18-52-15.png

                Detail: Changed button's name to exportBtn_2 and targetWidget to simulationWidget, but the error persists

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  Did you include QEvent ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  fabriciokashF 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Did you include QEvent ?

                    fabriciokashF Offline
                    fabriciokashF Offline
                    fabriciokash
                    wrote on last edited by
                    #13

                    @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

                    Did you include QEvent ?

                    Yes, i do.

                    fabriciokashF 1 Reply Last reply
                    0
                    • fabriciokashF fabriciokash

                      @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

                      Did you include QEvent ?

                      Yes, i do.

                      fabriciokashF Offline
                      fabriciokashF Offline
                      fabriciokash
                      wrote on last edited by
                      #14

                      @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

                      Did you include QEvent ?

                      I got the solution. I was doing #include <xdotool-master/xdo.h> and i think this file was causing some type of conflict. I just commented this line.

                      I gonna back to the event's problem

                      1 Reply Last reply
                      0
                      • fabriciokashF Offline
                        fabriciokashF Offline
                        fabriciokash
                        wrote on last edited by fabriciokash
                        #15
                        void Simulate::on_selectAllBtn_clicked()
                        {
                            QKeyEvent *eventPress = new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier);
                            QKeyEvent *eventRealease = new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::ControlModifier);
                            QCoreApplication::postEvent(targetWidget, eventPress);
                            QCoreApplication::postEvent(targetWidget, eventRealease);
                        }
                        

                        This solution doesnt work. Is this code correct? As I commented, I created a QWindow from a window ID of a external program (in case, a gedit text editor) and put it on a QWidget. I dont know if this approach is for my case. I tried too use QApplication::setActiveWIndow and targetWidget->setFocus() before, but without success. Tried send the event to QWindow too, without success.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by SGaist
                          #16

                          You are trying to send these events to an external application ?
                          Which is it ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          fabriciokashF 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            You are trying to send these events to an external application ?
                            Which is it ?

                            fabriciokashF Offline
                            fabriciokashF Offline
                            fabriciokash
                            wrote on last edited by fabriciokash
                            #17

                            @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

                            You are trying to send these events to an external application ?
                            Which is it ?

                            Kind of it. I have a gedit text editor (external application) embed to a QWindow (intern application). And a QWidget is created from this QWindow, using QWidget *widget = QWidget::createWindowContainer(window);

                            In addition to that widget, i have too, on GUI, a QButton, called "Select All". When user click on this button, the system should select all text in text editor. So Im trying to send a Ctrl+A under the covers.

                            But I dont know if Im on right way to do this using that code.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #18

                              Which OS are you on ?

                              Sending key events through Qt's event loop like that won't work AFAIK. The application while embedded is still an external application.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              fabriciokashF 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                Which OS are you on ?

                                Sending key events through Qt's event loop like that won't work AFAIK. The application while embedded is still an external application.

                                fabriciokashF Offline
                                fabriciokashF Offline
                                fabriciokash
                                wrote on last edited by
                                #19

                                @SGaist said in How to trigger a hotkey to a QWindow using a QButton?:

                                Which OS are you on ?
                                Sending key events through Qt's event loop like that won't work AFAIK. The application while embedded is still an external application.

                                Im using Ubuntu 18.04.

                                There is a way to send the key event through OS?

                                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