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.7k 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

    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