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 do I use QKeyEvent::key() even if window is not on top?
Forum Updated to NodeBB v4.3 + New Features

How do I use QKeyEvent::key() even if window is not on top?

Scheduled Pinned Locked Moved Unsolved General and Desktop
33 Posts 4 Posters 15.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.
  • raven-worxR raven-worx

    @SnuggleKat said in How do I use QKeyEvent::key() even if window is not on top?:

    I need it to be active while I'm outside the window or in the actual game.

    then Qt is not the right framework for you.
    By definition only the active GUI window gets the input events delivered.

    You need to hook into the OS using native API to achieve what you want.

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #4

    @raven-worx said in How do I use QKeyEvent::key() even if window is not on top?:

    then Qt is not the right framework for you.
    By definition only the active GUI window gets the input events delivered.

    You need to hook into the OS using native API to achieve what you want.

    I think there is a way to achieve this with Qt (and an API) since there is other programs developed with Qt that allow "Background Input" (e.g. Dolphin Emulator).

    I'm checking out Microsoft's API Documentation right now but I'm uncertain which Function is the right one: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645530(v=vs.85).aspx

    raven-worxR 1 Reply Last reply
    0
    • ? A Former User

      @raven-worx said in How do I use QKeyEvent::key() even if window is not on top?:

      then Qt is not the right framework for you.
      By definition only the active GUI window gets the input events delivered.

      You need to hook into the OS using native API to achieve what you want.

      I think there is a way to achieve this with Qt (and an API) since there is other programs developed with Qt that allow "Background Input" (e.g. Dolphin Emulator).

      I'm checking out Microsoft's API Documentation right now but I'm uncertain which Function is the right one: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645530(v=vs.85).aspx

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #5

      @SnuggleKat said in How do I use QKeyEvent::key() even if window is not on top?:

      I think there is a way to achieve this with Qt (and an API)

      thats what i said...
      Qt alone isn't capable of it. As i said you need to use native API to get the key events.
      Of course you can create artificial events at any time and inject them into Qt's event-loop.

      --- 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
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #6

        Hi
        it sounds like you want a global hot key
        https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx

        ? 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          it sounds like you want a global hot key
          https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #7

          @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

          Hi
          it sounds like you want a global hot key
          https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx

          This looks like what I need. Thanks!
          Using this results in 2 error messages:

          - mainwindow.obj:-1: Fehler: LNK2019: unresolved external symbol __imp_RegisterHotKey referenced in function "public: void __cdecl MainWindow::timer(void)" (?timer@MainWindow@@QEAAXXZ)
          - debug\Code_Manager.exe:-1: Fehler: LNK1120: 1 unresolved externals
          

          I have tried checking whether the b-key is pressed:

              if (RegisterHotKey(NULL, 1, MOD_ALT | MOD_NOREPEAT, 0x42) == TRUE)  //0x42 is 'b'
              {
                  ui->label->setText("true");
              }
              else
              {
                  ui->label->setText("false");
              }
          
          mrjjM 1 Reply Last reply
          0
          • ? A Former User

            @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

            Hi
            it sounds like you want a global hot key
            https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx

            This looks like what I need. Thanks!
            Using this results in 2 error messages:

            - mainwindow.obj:-1: Fehler: LNK2019: unresolved external symbol __imp_RegisterHotKey referenced in function "public: void __cdecl MainWindow::timer(void)" (?timer@MainWindow@@QEAAXXZ)
            - debug\Code_Manager.exe:-1: Fehler: LNK1120: 1 unresolved externals
            

            I have tried checking whether the b-key is pressed:

                if (RegisterHotKey(NULL, 1, MOD_ALT | MOD_NOREPEAT, 0x42) == TRUE)  //0x42 is 'b'
                {
                    ui->label->setText("true");
                }
                else
                {
                    ui->label->setText("false");
                }
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @SnuggleKat
            Hi
            The trick is always check which library to link to
            alt text

            Then add

            LIBS += -luser32

            to the .pro file.

            ? 1 Reply Last reply
            1
            • mrjjM mrjj

              @SnuggleKat
              Hi
              The trick is always check which library to link to
              alt text

              Then add

              LIBS += -luser32

              to the .pro file.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #9

              @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

              LIBS += -luser32

              Thanks, but the problem still remains the same

              mrjjM 1 Reply Last reply
              0
              • ? A Former User

                @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                LIBS += -luser32

                Thanks, but the problem still remains the same

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #10

                @SnuggleKat
                Remember to run qmake after adding it
                Up in the build menu.

                ? 1 Reply Last reply
                0
                • mrjjM mrjj

                  @SnuggleKat
                  Remember to run qmake after adding it
                  Up in the build menu.

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by A Former User
                  #11

                  @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                  @SnuggleKat
                  Remember to run qmake after adding it
                  Up in the build menu.

                  Thanks. I could compile it now. But the key value doesn't get checked. Could this issue be caused by the function being inside called up by a QTimer?
                  If I put the function into the MainWindow::MainWindow the RegisterHotKey permanently returns TRUE

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

                    Hi
                    You mean from a timer slot ?
                    Should not make a difference however if you call it multiple times t without -
                    UnregisterHotKey , it might considered it already registered and return false.
                    you can show what GetLastError says in that case.

                    ? 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      Hi
                      You mean from a timer slot ?
                      Should not make a difference however if you call it multiple times t without -
                      UnregisterHotKey , it might considered it already registered and return false.
                      you can show what GetLastError says in that case.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #13

                      @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                      Hi
                      You mean from a timer slot ?
                      Should not make a difference however if you call it multiple times t without -
                      UnregisterHotKey , it might considered it already registered and return false.
                      you can show what GetLastError says in that case.

                      Well, I got it working while the window is on top. But once I leave the window the button isn't registered anymore.

                      RegisterHotKey(NULL, 0, MOD_IGNORE_ALL_MODIFIER, 0x42);
                          MSG msg;
                          GetMessage(&msg, 0, 0, 0);
                          PeekMessage(&msg, 0, 0, 0, 0x0001);
                      
                          if(msg.wParam == 0x42)  //0x42 is 'b'
                          {
                              ui->label->setText("true");
                              *(cheat_array + 16) = 0x11;
                          }
                          else
                          {
                              ui->label->setText("false");
                              *(cheat_array + 16) = 0x00;
                          }
                      
                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        Hi
                        You are using NULL for windows handle
                        Docs says
                        "If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop."

                        So i am wondering if it gets delivered by Qt when its not active.

                        Try to use its
                        (HWND)winId();
                        instead of NULL

                        ? 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          You are using NULL for windows handle
                          Docs says
                          "If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop."

                          So i am wondering if it gets delivered by Qt when its not active.

                          Try to use its
                          (HWND)winId();
                          instead of NULL

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #15

                          @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                          (HWND)winId();

                          no change

                          mrjjM 1 Reply Last reply
                          0
                          • ? A Former User

                            @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                            (HWND)winId();

                            no change

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            @SnuggleKat
                            Ok, i think its related to how the events are sent when windows not
                            in focus.

                            try use eventfilter for native events and see if it comes there when not in focus.
                            https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilter

                            you can also try this sample ( non qt) and see if that works in all cases.
                            Its uses native msg pump.

                            #include <stdio.h>
                            #include <tchar.h>
                            #include <windows.h>
                            
                            int main()
                            {       
                            	enum{ONE_KEYID = 1, TWO_KEYID = 2};
                            	RegisterHotKey(0, ONE_KEYID, MOD_NOREPEAT, 0x31); // register 1 key as hotkey
                            	RegisterHotKey(0, TWO_KEYID, MOD_NOREPEAT, 0x32); // register 2 key as hotkey
                            	MSG msg;
                            	while(GetMessage(&msg, 0, 0, 0))
                            	{
                            		PeekMessage(&msg, 0, 0, 0, 0x0001);
                            		switch(msg.message)
                            		{
                            		case WM_HOTKEY:
                            			if(msg.wParam == ONE_KEYID)
                            			{
                            				printf("1 Pressed");
                            			}
                            			else if(msg.wParam == TWO_KEYID)
                            			{
                            				printf("2 Pressed");
                            			}
                            		}
                            	}
                            	return 0;
                            }
                            
                            ? 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @SnuggleKat
                              Ok, i think its related to how the events are sent when windows not
                              in focus.

                              try use eventfilter for native events and see if it comes there when not in focus.
                              https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilter

                              you can also try this sample ( non qt) and see if that works in all cases.
                              Its uses native msg pump.

                              #include <stdio.h>
                              #include <tchar.h>
                              #include <windows.h>
                              
                              int main()
                              {       
                              	enum{ONE_KEYID = 1, TWO_KEYID = 2};
                              	RegisterHotKey(0, ONE_KEYID, MOD_NOREPEAT, 0x31); // register 1 key as hotkey
                              	RegisterHotKey(0, TWO_KEYID, MOD_NOREPEAT, 0x32); // register 2 key as hotkey
                              	MSG msg;
                              	while(GetMessage(&msg, 0, 0, 0))
                              	{
                              		PeekMessage(&msg, 0, 0, 0, 0x0001);
                              		switch(msg.message)
                              		{
                              		case WM_HOTKEY:
                              			if(msg.wParam == ONE_KEYID)
                              			{
                              				printf("1 Pressed");
                              			}
                              			else if(msg.wParam == TWO_KEYID)
                              			{
                              				printf("2 Pressed");
                              			}
                              		}
                              	}
                              	return 0;
                              }
                              
                              ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #17

                              @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                              @SnuggleKat
                              Ok, i think its related to how the events are sent when windows not
                              in focus.

                              try use eventfilter for native events and see if it comes there when not in focus.
                              https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilter

                              you can also try this sample ( non qt) and see if that works in all cases.
                              Its uses native msg pump.

                              #include <stdio.h>
                              #includ...
                              

                              My recent code update was based on this example.
                              Tried rebuilding it but didn't work either.

                              And I'm a little bit lost about the EventFilter thing..

                              mrjjM 1 Reply Last reply
                              0
                              • ? A Former User

                                @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                                @SnuggleKat
                                Ok, i think its related to how the events are sent when windows not
                                in focus.

                                try use eventfilter for native events and see if it comes there when not in focus.
                                https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilter

                                you can also try this sample ( non qt) and see if that works in all cases.
                                Its uses native msg pump.

                                #include <stdio.h>
                                #includ...
                                

                                My recent code update was based on this example.
                                Tried rebuilding it but didn't work either.

                                And I'm a little bit lost about the EventFilter thing..

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #18

                                @SnuggleKat

                                The link have sample of such eventfiler.
                                its not much code.

                                Its odd, the other sample didnt work as its windows less and have message pump.
                                But if u mix with a normal Qt it makes sense as the events are eating by QApplication then.

                                ? 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @SnuggleKat

                                  The link have sample of such eventfiler.
                                  its not much code.

                                  Its odd, the other sample didnt work as its windows less and have message pump.
                                  But if u mix with a normal Qt it makes sense as the events are eating by QApplication then.

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #19

                                  @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                                  @SnuggleKat

                                  The link have sample of such eventfiler.
                                  its not much code.

                                  Its odd, the other sample didnt work as its windows less and have message pump.
                                  But if u mix with a normal Qt it makes sense as the events are eating by QApplication then.

                                  I'm sorry, I can't get it to compile..

                                  Could it be it doesn't work because the timer() function is a (public) slot?

                                  mrjjM 1 Reply Last reply
                                  0
                                  • ? A Former User

                                    @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                                    @SnuggleKat

                                    The link have sample of such eventfiler.
                                    its not much code.

                                    Its odd, the other sample didnt work as its windows less and have message pump.
                                    But if u mix with a normal Qt it makes sense as the events are eating by QApplication then.

                                    I'm sorry, I can't get it to compile..

                                    Could it be it doesn't work because the timer() function is a (public) slot?

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #20

                                    @SnuggleKat
                                    A slot is just a normal c++ function.

                                    But i dont understand why you need to call it in a timer.
                                    You normally call it ONLY once to set up hot key.
                                    Then reacts to WM_HOTKEY native event.
                                    There would be no need of a timer.

                                    Mixing Qt application and GetMessage() might have side effects.

                                    This might help
                                    http://amin-ahmadi.com/2015/11/14/how-to-use-system-wide-hotkeys-in-your-qt-application/

                                    ? 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @SnuggleKat
                                      A slot is just a normal c++ function.

                                      But i dont understand why you need to call it in a timer.
                                      You normally call it ONLY once to set up hot key.
                                      Then reacts to WM_HOTKEY native event.
                                      There would be no need of a timer.

                                      Mixing Qt application and GetMessage() might have side effects.

                                      This might help
                                      http://amin-ahmadi.com/2015/11/14/how-to-use-system-wide-hotkeys-in-your-qt-application/

                                      ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #21

                                      @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                                      @SnuggleKat
                                      A slot is just a normal c++ function.

                                      But i dont understand why you need to call it in a timer.
                                      You normally call it ONLY once to set up hot key.
                                      Then reacts to WM_HOTKEY native event.
                                      There would be no need of a timer.

                                      Mixing Qt application and GetMessage() might have side effects.

                                      This might help
                                      http://amin-ahmadi.com/2015/11/14/how-to-use-system-wide-hotkeys-in-your-qt-application/

                                      I need it in a timer because it executes the codehandler as many times a second as defined by the user.
                                      However, I could also transfer the key_value or key_code over to the timer

                                      1 Reply Last reply
                                      0
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #22

                                        Ok. but try the sample i linked. it is using virtual override for native Event and it might work when app is not focused.
                                        Else an event filter on application seems the best way to grab WM_HOTKEY.

                                        ? 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          Ok. but try the sample i linked. it is using virtual override for native Event and it might work when app is not focused.
                                          Else an event filter on application seems the best way to grab WM_HOTKEY.

                                          ? Offline
                                          ? Offline
                                          A Former User
                                          wrote on last edited by
                                          #23

                                          @mrjj said in How do I use QKeyEvent::key() even if window is not on top?:

                                          Ok. but try the sample i linked. it is using virtual override for native Event and it might work when app is not focused.
                                          Else an event filter on application seems the best way to grab WM_HOTKEY.

                                          Wow, it works, thank you!
                                          For some reason it makes the b-key not typing anything. is there are workaround?

                                          mrjjM 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