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.2k 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.
  • ? 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
                              • ? A Former User

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

                                @SnuggleKat
                                Super :)
                                Some other app might have requested a hotkey on b.
                                So if be is not sent to you. it means something else takes it.
                                I think F12 also reserved and some others.

                                ? 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @SnuggleKat
                                  Super :)
                                  Some other app might have requested a hotkey on b.
                                  So if be is not sent to you. it means something else takes it.
                                  I think F12 also reserved and some others.

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

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

                                  @SnuggleKat
                                  Super :)
                                  Some other app might have requested a hotkey on b.
                                  So if be is not sent to you. it means something else takes it.
                                  I think F12 also reserved and some others.

                                  I see!
                                  Well, it's not too bad though. I will upload a demo once I have made some more progress with my application

                                  mrjjM 1 Reply Last reply
                                  1
                                  • ? A Former User

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

                                    @SnuggleKat
                                    Super :)
                                    Some other app might have requested a hotkey on b.
                                    So if be is not sent to you. it means something else takes it.
                                    I think F12 also reserved and some others.

                                    I see!
                                    Well, it's not too bad though. I will upload a demo once I have made some more progress with my application

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

                                    @SnuggleKat
                                    Super. please mark as solved if possible.
                                    You can always open other if later questions comes up.

                                    ? 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @SnuggleKat
                                      Super. please mark as solved if possible.
                                      You can always open other if later questions comes up.

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

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

                                      @SnuggleKat
                                      Super. please mark as solved if possible.
                                      You can always open other if later questions comes up.

                                      Here's a little demo video!
                                      https://twitter.com/CosmoCortney/status/929459711388344322

                                      Well, there came up another problem tho..
                                      when I pass another key value to the RegisterHotKey() function any of the both values trigger both functions at the same time.
                                      I think I should un-register the value after it's corresponding function was executed. but how do I do this?

                                      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
                                        Super. please mark as solved if possible.
                                        You can always open other if later questions comes up.

                                        Here's a little demo video!
                                        https://twitter.com/CosmoCortney/status/929459711388344322

                                        Well, there came up another problem tho..
                                        when I pass another key value to the RegisterHotKey() function any of the both values trigger both functions at the same time.
                                        I think I should un-register the value after it's corresponding function was executed. but how do I do this?

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

                                        @SnuggleKat
                                        Hehe cool. Not sure what the reindeer is doing though :)

                                        Hi
                                        If you register 2 hotkeys. it will call nativeEvent twice regardless of which
                                        key you press or what do u mean ?

                                        if you read the docs, it says
                                        "lParam
                                        The low-order word specifies the keys that were to be pressed in combination with the key specified by the high-order word to generate the WM_HOTKEY message. This word can be one or more of the following values. The high-order word specifies the virtual key code of the hot key."

                                        So maybe you just need to alter the code and only call your function depending on what actual hot key is?

                                        Like

                                        #define VK_M 0x4D
                                        #define VK_N 0x4E
                                        
                                        MainWindow::MainWindow(QWidget* parent) :
                                          QMainWindow(parent),
                                          ui(new Ui::MainWindow) {
                                          ui->setupUi(this);
                                        
                                          if(!RegisterHotKey(HWND(winId()), 0, MOD_ALT | MOD_CONTROL, VK_M)) {
                                            QMessageBox::warning(this, "Warning", "Can’t register hotkey ALT + CTRL + M");
                                          }
                                        
                                          if(!RegisterHotKey(HWND(winId()), 0, MOD_ALT | MOD_CONTROL, VK_N)) {
                                            QMessageBox::warning(this, "Warning", "Can’t register hotkey ALT + CTRL + N");
                                          }
                                        }
                                        
                                        bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result) {
                                          MSG* msg = static_cast<MSG*>(message);
                                          if(msg->message == WM_HOTKEY) {
                                            WORD hotKey =  HIWORD(msg->lParam);
                                            switch (hotKey) {
                                              case VK_M:
                                                QMessageBox::about(this, "", "VM_M pressed");
                                                break;
                                              case VK_N:
                                                QMessageBox::about(this, "", "VM_N pressed");
                                                break;
                                              default:
                                                break;
                                            }
                                            return true;
                                          }
                                          return false;
                                        }
                                        
                                        ? 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @SnuggleKat
                                          Hehe cool. Not sure what the reindeer is doing though :)

                                          Hi
                                          If you register 2 hotkeys. it will call nativeEvent twice regardless of which
                                          key you press or what do u mean ?

                                          if you read the docs, it says
                                          "lParam
                                          The low-order word specifies the keys that were to be pressed in combination with the key specified by the high-order word to generate the WM_HOTKEY message. This word can be one or more of the following values. The high-order word specifies the virtual key code of the hot key."

                                          So maybe you just need to alter the code and only call your function depending on what actual hot key is?

                                          Like

                                          #define VK_M 0x4D
                                          #define VK_N 0x4E
                                          
                                          MainWindow::MainWindow(QWidget* parent) :
                                            QMainWindow(parent),
                                            ui(new Ui::MainWindow) {
                                            ui->setupUi(this);
                                          
                                            if(!RegisterHotKey(HWND(winId()), 0, MOD_ALT | MOD_CONTROL, VK_M)) {
                                              QMessageBox::warning(this, "Warning", "Can’t register hotkey ALT + CTRL + M");
                                            }
                                          
                                            if(!RegisterHotKey(HWND(winId()), 0, MOD_ALT | MOD_CONTROL, VK_N)) {
                                              QMessageBox::warning(this, "Warning", "Can’t register hotkey ALT + CTRL + N");
                                            }
                                          }
                                          
                                          bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result) {
                                            MSG* msg = static_cast<MSG*>(message);
                                            if(msg->message == WM_HOTKEY) {
                                              WORD hotKey =  HIWORD(msg->lParam);
                                              switch (hotKey) {
                                                case VK_M:
                                                  QMessageBox::about(this, "", "VM_M pressed");
                                                  break;
                                                case VK_N:
                                                  QMessageBox::about(this, "", "VM_N pressed");
                                                  break;
                                                default:
                                                  break;
                                              }
                                              return true;
                                            }
                                            return false;
                                          }
                                          
                                          ? Offline
                                          ? Offline
                                          A Former User
                                          wrote on last edited by A Former User
                                          #29

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

                                          @SnuggleKat
                                          Hehe cool. Not sure what the reindeer is doing though :)

                                          Hi
                                          If you register 2 hotkeys. it will call nativeEvent twice regardless of which
                                          key you press or what do u mean ?

                                          if you read the docs, it says
                                          "lParam
                                          The low-order word specifies the keys that were to be pressed in combination with the key specified by the high-order word to generate the WM_HOTKEY message. This word can be one or more of the following values. The high-order word specifies the virtual key code of the hot key."

                                          So maybe you just need to alter the code and only call your function depending on what actual hot key is?

                                          Thanks!
                                          What i did was loading the reindeer's x, y and z size values (floats) and incremented it and stored it back to the game each time the timer repeated. This action was triggered by the hotkey =)

                                          I have modified the code a lot in order to work as I need it:

                                          void MainWindow::if_key_pressed() // 080000XX 000000VV // this is what the cheat code layout looks like. VV = hotkey value
                                                                         // D00000XX BADF000D // this is the end-if code where to branch when the hot key is not pressed.
                                          {  
                                           key_value = *(cheat_array + index_B + 0x07);
                                              ifnr = *(cheat_array + index_B + 0x03);
                                          
                                          
                                              RegisterHotKey(HWND(winId()), 0, 0, key_value); // register hotkey
                                          
                                              if(key_is_pressed == false)
                                              {
                                                  index_B += 0x08;
                                                  for(int i = 0, j = index_B; (*(cheat_array + j + i) != 0xD0) || (*(cheat_array + j + i + 3) != ifnr); i += 8)
                                                  {
                                                      index_B += 8;
                                                  }
                                              }
                                              else
                                              {
                                                  index_B += 0x08;
                                              }
                                          }
                                          
                                          //....
                                          
                                          bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
                                          {
                                              Q_UNUSED(eventType);
                                              Q_UNUSED(result);
                                              MSG *msg = static_cast<MSG*>(message);
                                              if(msg->message == WM_HOTKEY) // check if key is pressed
                                              {
                                                  key_is_pressed = true;
                                                  true;
                                              }
                                              else
                                              {
                                                  key_is_pressed = false;
                                                  false;
                                              }
                                              return false;
                                          }
                                          
                                          
                                          // if the key is pressed index_B will jump to the cheats to be executed (e.g. to grow the deer). if it's not pressed it jumps to the end-if to skip the cheats
                                          

                                          so.. should I try clear msg?

                                          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