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.1k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 10 Nov 2017, 11:34 last edited by
    #1

    Hello there,

    I've been developing a sort of code manager where functions are set and activated by hex codes (It's pretty much an esoteric programming language with an actual use lol).
    Right now I'm coding a function to check if a certain key is pressed in order to enable the following instruction(s).
    i can already get the current key's value when the window is on top. But it stops working as soon as I leave the window.
    I need it to be active while I'm outside the window or in the actual game.

    For testing purposes I'd like to get this code running while the window is not on top.

    void MainWindow::keyPressEvent(QKeyEvent *ckey)
    {
        ui->label->setText(QString::number(ckey->key(), 16) + " " + ckey->text() + " " + QString::number(ckey->nativeScanCode(), 16));
    }
    

    The application also has a QTimer that is permanently active even if the window is not on top. Maybe this can be used as a reference point here...

    Would anyone like to help me?
    Thanks

    R 1 Reply Last reply 10 Nov 2017, 12:55
    0
    • ? A Former User
      10 Nov 2017, 11:34

      Hello there,

      I've been developing a sort of code manager where functions are set and activated by hex codes (It's pretty much an esoteric programming language with an actual use lol).
      Right now I'm coding a function to check if a certain key is pressed in order to enable the following instruction(s).
      i can already get the current key's value when the window is on top. But it stops working as soon as I leave the window.
      I need it to be active while I'm outside the window or in the actual game.

      For testing purposes I'd like to get this code running while the window is not on top.

      void MainWindow::keyPressEvent(QKeyEvent *ckey)
      {
          ui->label->setText(QString::number(ckey->key(), 16) + " " + ckey->text() + " " + QString::number(ckey->nativeScanCode(), 16));
      }
      

      The application also has a QTimer that is permanently active even if the window is not on top. Maybe this can be used as a reference point here...

      Would anyone like to help me?
      Thanks

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 10 Nov 2017, 12:55 last edited by
      #2

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

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

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

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

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

      ? 1 Reply Last reply 10 Nov 2017, 13:35
      1
      • M Offline
        M Offline
        mostefa
        wrote on 10 Nov 2017, 13:06 last edited by
        #3

        Hi @SnuggleKat

        What if you install an eventFilter ?

        There is an example in the link:

        class KeyPressEater : public QObject
        {
            Q_OBJECT
            ...
        
        protected:
            bool eventFilter(QObject *obj, QEvent *event);
        };
        
        bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
        {
            if (event->type() == QEvent::KeyPress) {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                qDebug("Ate key press %d", keyEvent->key());
                return true;
            } else {
                // standard event processing
                return QObject::eventFilter(obj, event);
            }
        }
        

        And here's how to install it on two widgets:

        KeyPressEater *keyPressEater = new KeyPressEater(this);
        QPushButton *pushButton = new QPushButton(this);
        QListView *listView = new QListView(this);
        
        pushButton->installEventFilter(keyPressEater);
        listView->installEventFilter(keyPressEater);
        
        1 Reply Last reply
        0
        • R raven-worx
          10 Nov 2017, 12:55

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

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

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

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

          ? Offline
          ? Offline
          A Former User
          wrote on 10 Nov 2017, 13:35 last edited by
          #4

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

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

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

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

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

          R 1 Reply Last reply 10 Nov 2017, 13:40
          0
          • ? A Former User
            10 Nov 2017, 13:35

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

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

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

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

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

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 10 Nov 2017, 13:40 last edited by
            #5

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

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

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

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

            1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 10 Nov 2017, 15:47 last edited by
              #6

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

              ? 1 Reply Last reply 10 Nov 2017, 17:32
              1
              • mrjjM mrjj
                10 Nov 2017, 15:47

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

                ? Offline
                ? Offline
                A Former User
                wrote on 10 Nov 2017, 17:32 last edited by
                #7

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

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

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

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

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

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

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

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

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

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

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

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

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

                  Then add

                  LIBS += -luser32

                  to the .pro file.

                  ? 1 Reply Last reply 10 Nov 2017, 18:08
                  1
                  • mrjjM mrjj
                    10 Nov 2017, 18:02

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

                    Then add

                    LIBS += -luser32

                    to the .pro file.

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on 10 Nov 2017, 18:08 last edited by
                    #9

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

                    LIBS += -luser32

                    Thanks, but the problem still remains the same

                    mrjjM 1 Reply Last reply 10 Nov 2017, 18:11
                    0
                    • ? A Former User
                      10 Nov 2017, 18:08

                      @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 10 Nov 2017, 18:11 last edited by mrjj 11 Oct 2017, 18:12
                      #10

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

                      ? 1 Reply Last reply 10 Nov 2017, 19:29
                      0
                      • mrjjM mrjj
                        10 Nov 2017, 18:11

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

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on 10 Nov 2017, 19:29 last edited by A Former User 11 Oct 2017, 19:33
                        #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 11 Nov 2017, 07:41 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 11 Nov 2017, 07:52
                          0
                          • mrjjM mrjj
                            11 Nov 2017, 07:41

                            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 11 Nov 2017, 07:52 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 11 Nov 2017, 08:20 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 11 Nov 2017, 08:53
                              0
                              • mrjjM mrjj
                                11 Nov 2017, 08:20

                                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 11 Nov 2017, 08:53 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 11 Nov 2017, 10:43
                                0
                                • ? A Former User
                                  11 Nov 2017, 08:53

                                  @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 11 Nov 2017, 10:43 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 11 Nov 2017, 12:24
                                  0
                                  • mrjjM mrjj
                                    11 Nov 2017, 10:43

                                    @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 11 Nov 2017, 12:24 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 11 Nov 2017, 12:35
                                    0
                                    • ? A Former User
                                      11 Nov 2017, 12:24

                                      @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 11 Nov 2017, 12:35 last edited by mrjj 11 Nov 2017, 12:35
                                      #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 11 Nov 2017, 13:33
                                      0
                                      • mrjjM mrjj
                                        11 Nov 2017, 12:35

                                        @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 11 Nov 2017, 13:33 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 11 Nov 2017, 13:49
                                        0
                                        • ? A Former User
                                          11 Nov 2017, 13:33

                                          @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 11 Nov 2017, 13:49 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 11 Nov 2017, 13:56
                                          0

                                          1/33

                                          10 Nov 2017, 11:34

                                          • Login

                                          • Login or register to search.
                                          1 out of 33
                                          • First post
                                            1/33
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved