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.4k 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.
  • 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
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #30

                                  Hi
                                  ok. not sure what question is ?
                                  code will do the same regardless of what you register as hotkey.
                                  Is that what you wanted ?

                                  ? 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    Hi
                                    ok. not sure what question is ?
                                    code will do the same regardless of what you register as hotkey.
                                    Is that what you wanted ?

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

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

                                    Hi
                                    ok. not sure what question is ?
                                    code will do the same regardless of what you register as hotkey.
                                    Is that what you wanted ?

                                    Sorry for my absence, I had a busy month...
                                    The problem is that once the key was pressed key_is_pressed stays true even when I release the key. So the function I call up with the pressed key keeps being active.
                                    This is what I'd need to have fixed so I can control the flow of the code with the keys

                                    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
                                      ok. not sure what question is ?
                                      code will do the same regardless of what you register as hotkey.
                                      Is that what you wanted ?

                                      Sorry for my absence, I had a busy month...
                                      The problem is that once the key was pressed key_is_pressed stays true even when I release the key. So the function I call up with the pressed key keeps being active.
                                      This is what I'd need to have fixed so I can control the flow of the code with the keys

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

                                      @SnuggleKat
                                      Ok try my code. did not notice it always being true.
                                      You register the shortcut on each key press. Its not how its ment to work.
                                      (i call it once in constructor)

                                      Also you must use
                                      WORD hotKey = HIWORD(msg->lParam);
                                      and not lParam directly.

                                      ? 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        @SnuggleKat
                                        Ok try my code. did not notice it always being true.
                                        You register the shortcut on each key press. Its not how its ment to work.
                                        (i call it once in constructor)

                                        Also you must use
                                        WORD hotKey = HIWORD(msg->lParam);
                                        and not lParam directly.

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

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

                                        @SnuggleKat
                                        Ok try my code. did not notice it always being true.
                                        You register the shortcut on each key press. Its not how its ment to work.
                                        (i call it once in constructor)

                                        Also you must use
                                        WORD hotKey = HIWORD(msg->lParam);
                                        and not lParam directly.

                                        Thanks,
                                        I had to modify it a little bit because the user can define which key to be checked (case labels don't like variables).

                                        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
                                            {
                                                WORD hotKey =  HIWORD(msg->lParam);
                                                if(hotKey == key_value)
                                                {
                                                    key_is_pressed = true;
                                                    true;
                                                    setWindowTitle("pressed");
                                                }
                                                else
                                                {
                                                    key_is_pressed = false;
                                                    false;
                                                    setWindowTitle("not pressed");
                                                }
                                            }
                                            return false;
                                        }
                                        

                                        It behaves weird now.
                                        If I'm checking a certain user defined key value the function will stay on once the corresponding key was hit. (same as before)
                                        If I'm checking for more than one value where each value triggers a different function the earlier noted values will turn off everything and the last one will turn all functions on.

                                        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