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. [SOLVED]How to simulate a key sequence?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to simulate a key sequence?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 5.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.
  • musimbateM Offline
    musimbateM Offline
    musimbate
    wrote on last edited by
    #1

    Hi all,
    I am trying to simulate a key combination like "Ctrl + Shift + M " in my application but nothing happens when i call the function that does the job.My function is as follows:
    @
    void XXXXXXXXX::hideAndSimulateKeyPress()
    {

    mMainWindow->actionHideApplication->trigger();
    //THIS IS MY CODE TO SIMULATE THE COMBINATION .MAY BE THERE IS SOMETHING WRONG...
    QTest::keyClick(UBApplication::desktop(), Qt::Key_M,Qt::ControlModifier&Qt::ShiftModifier);

    }

    @
    Thanks for your time.

    Why join the navy if you can be a pirate?-Steve Jobs

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      do you write a unit test case or do you use QTest in your regular application?
      Why don't you also create a QAction which you do trigger manually. Additionally you set a application wide shortcut on it in case the user triggers it with the key combination?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • musimbateM Offline
        musimbateM Offline
        musimbate
        wrote on last edited by
        #3

        Thanks raven-works for your answer.

        [quote author="raven-worx" date="1371192068"]do you write a unit test case or do you use QTest in your regular application?[/quote]
        I use it in my regular application on a temporary basis

        [quote author="raven-worx" date="1371192068"]
        Why don't you also create a QAction which you do trigger manually. ?[/quote]

        I want the combination to be trigered (simulated) internally from my application(Design requirement).

        But the QTest::keyClick should work right?Haven't figured out yet what i am doing wrong.

        Thanks again for your time.

        Why join the navy if you can be a pirate?-Steve Jobs

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          it should work, yes. But you shouldn't send it to the QDesktopWidget ;)
          I don't know how and where you catch the key event ("Ctrl+Shift+M")...
          If you catch it in a child widget and send it to your mainwindow the child widget will never receive the event.
          As i said you can create a QAction with an application widget shortcut set, or you add an event filter on the QApplication instance and look for the keyevent there ...

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • musimbateM Offline
            musimbateM Offline
            musimbate
            wrote on last edited by
            #5

            Sorry may be I am not explaining well my problem.Here is the situation:
            .I have a (separate) application that pops us a menu when we do the combination (“Ctrl+Shift+M”) on the keyboard.

            .I have an action in my application that does the following when triggered:

            -->>hide the application so the desktop screen has focus(That is why I retrieved a pointer to the desktop in the first argument of keyClick).

            -->>simulate the combination so we have the same effect as if the user had done the combination on the keyboard.

            Is there a way in Qt to retrieve a pointer to the desktop?
            Edit: It is not my application that handles the event.
            I apologize if this is long and thanks for your time.

            Why join the navy if you can be a pirate?-Steve Jobs

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              well that's not possible, at least with Qt only.
              You will need to post the event/message OS API to send the appropriate event to the current active application.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • musimbateM Offline
                musimbateM Offline
                musimbate
                wrote on last edited by
                #7

                Thanks ,Guess I ll keep looking for another solution then.

                Why join the navy if you can be a pirate?-Steve Jobs

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  What is weird to me, is this:
                  @
                  QTest::keyClick(UBApplication::desktop(), Qt::Key_M,Qt::ControlModifier&Qt::ShiftModifier);
                  @

                  Why do you use the & operator? I think you need the | operator instead. Using & will result in a 0 argument there.

                  1 Reply Last reply
                  0
                  • musimbateM Offline
                    musimbateM Offline
                    musimbate
                    wrote on last edited by
                    #9

                    [quote author="Andre" date="1371197813"]What is weird to me, is this:

                    Why do you use the & operator? I think you need the | operator instead. Using & will result in a 0 argument there.[/quote]

                    I wanted to follow the examples from the book I use as my Qt reference.I know these are some hex bitwise operations but I kind of was lazy to go dig more.

                    Any way I was able to solve my problem using some brute force but at least it works for now.The problem was finding a widget to pass as argument in the keyClick function.
                    Since the application that processes the event was not my Qt application that sent it ,I was told it was impossible to find that pointer.

                    But I knew I have done something similar a few months ago on a Delphi application I was working on.So the idea came to me to write a small program to simulate the key combination (It doesn't require to pass that pointer) and start it using QProcess in my slot.

                    It works as I want but I want to know what more knowledgeable people think about this.And I hope it helps some one with the same issues.

                    Thanks guys.

                    Why join the navy if you can be a pirate?-Steve Jobs

                    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