Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. SendInput() does not work after GetMessage()

SendInput() does not work after GetMessage()

Scheduled Pinned Locked Moved Windows API
helpannouncement
4 Posts 3 Posters 10 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.
  • S Offline
    S Offline
    sashok_bg
    wrote on last edited by
    #1

    Hello everyone, I was trying to make an application, that registers a hotkey and when the hotkey is pressed, simulates a keyboard event or mouse click. To do so, I have chosen to use the RegisterHotkey(), SendInput() and GetMessage() windows functions. The problem is that as soon as I declare a call to GetMessage() - SendInput() stops working. In the code below, I made an experiment - the program is waiting for 3 seconds (to give me some time) and as soon as I start it, I bring notepad to focus and the "KeyDown(97)" types "1" in the notepad window. However, this behavior does not work when the MessageLoop() is entered. Thank you for your help. Best Regards, Alex

    #define _WIN32_WINNT 0x0500

    #include "stdafx.h"
    #include //This function simulates a keyboard button press and release. Argument is VirtualKey
    //This function works fine, before the GetMessage() function is declared/called
    void KeyDown(int vk){

    printf("Called KeyDown with parameter %d \\n",vk);
    
    INPUT	Input\[1\];
    
    Input\[0\].type  = INPUT\_KEYBOARD;
    Input\[0\].ki.wVk= vk;
    
    printf("Sending Input\\n");
    SendInput(1, Input, sizeof(Input));
    

    }

    //This function is blocking and waiting for a message to arrive
    void MessageLoop(){
    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0) != 0)
    {

    	//if the arrived message is a hotkey event
        if (msg.message == WM\_HOTKEY)
        {
    		//some debugging
    		printf("msg.message %d, msg.lParam %d, msg.wParam %d \\n",msg.message,msg.lParam,msg.wParam);
    
    		//press the numeric '1' on the keyboard - doesn't work
    		KeyDown(97);
        }
    
    	TranslateMessage(&msg);
    	DispatchMessage(&msg);
    }
    

    }

    int main(int argc, char* argv[])
    {

    //This part works, only when the GetMessage() function is in a separate function ex: MessageLoop----
    Sleep(3000);
    KeyDown(97);
    
    //Register an ALT-CTR-A hotkey with ID - 1. First argument is NULL to make the hotkey global (it is a hWnd)
    RegisterHotKey(
    	NULL,
    	1,
    	MOD\_CONTROL | MOD\_ALT,
    	0x41
    );  //0x41 is 'a'
    
    //Enter the message loop that blocks and waits for a message to arrive for this application
    
    //Note that if I put the 'while (GetMessage(&msg, NULL, 0, 0) != 0)' code here, 
    //instead of putting it in a separate function, 
    //the 'KeyDown(97)' call on the 2nd line of the main() function will not work !
    MessageLoop();
    
    //clean up
    UnregisterHotKey(NULL,1);
    
    return 0;
    

    }

    D J 2 Replies Last reply
    0
    • S sashok_bg

      Hello everyone, I was trying to make an application, that registers a hotkey and when the hotkey is pressed, simulates a keyboard event or mouse click. To do so, I have chosen to use the RegisterHotkey(), SendInput() and GetMessage() windows functions. The problem is that as soon as I declare a call to GetMessage() - SendInput() stops working. In the code below, I made an experiment - the program is waiting for 3 seconds (to give me some time) and as soon as I start it, I bring notepad to focus and the "KeyDown(97)" types "1" in the notepad window. However, this behavior does not work when the MessageLoop() is entered. Thank you for your help. Best Regards, Alex

      #define _WIN32_WINNT 0x0500

      #include "stdafx.h"
      #include //This function simulates a keyboard button press and release. Argument is VirtualKey
      //This function works fine, before the GetMessage() function is declared/called
      void KeyDown(int vk){

      printf("Called KeyDown with parameter %d \\n",vk);
      
      INPUT	Input\[1\];
      
      Input\[0\].type  = INPUT\_KEYBOARD;
      Input\[0\].ki.wVk= vk;
      
      printf("Sending Input\\n");
      SendInput(1, Input, sizeof(Input));
      

      }

      //This function is blocking and waiting for a message to arrive
      void MessageLoop(){
      MSG msg = {0};
      while (GetMessage(&msg, NULL, 0, 0) != 0)
      {

      	//if the arrived message is a hotkey event
          if (msg.message == WM\_HOTKEY)
          {
      		//some debugging
      		printf("msg.message %d, msg.lParam %d, msg.wParam %d \\n",msg.message,msg.lParam,msg.wParam);
      
      		//press the numeric '1' on the keyboard - doesn't work
      		KeyDown(97);
          }
      
      	TranslateMessage(&msg);
      	DispatchMessage(&msg);
      }
      

      }

      int main(int argc, char* argv[])
      {

      //This part works, only when the GetMessage() function is in a separate function ex: MessageLoop----
      Sleep(3000);
      KeyDown(97);
      
      //Register an ALT-CTR-A hotkey with ID - 1. First argument is NULL to make the hotkey global (it is a hWnd)
      RegisterHotKey(
      	NULL,
      	1,
      	MOD\_CONTROL | MOD\_ALT,
      	0x41
      );  //0x41 is 'a'
      
      //Enter the message loop that blocks and waits for a message to arrive for this application
      
      //Note that if I put the 'while (GetMessage(&msg, NULL, 0, 0) != 0)' code here, 
      //instead of putting it in a separate function, 
      //the 'KeyDown(97)' call on the 2nd line of the main() function will not work !
      MessageLoop();
      
      //clean up
      UnregisterHotKey(NULL,1);
      
      return 0;
      

      }

      D Offline
      D Offline
      Dhiraj Pallin
      wrote on last edited by
      #2

      I think you haven't initialised Input properly. Try memset(Input, 0, sizeof(Input)); Also note that Global Hotkeys trigger on key down, so your hot key might still be held down when you call SendInput(). I normally use something like GetAsyncKeyState() or create a keyboard hook to check when the hotkey is released.

      S 1 Reply Last reply
      0
      • D Dhiraj Pallin

        I think you haven't initialised Input properly. Try memset(Input, 0, sizeof(Input)); Also note that Global Hotkeys trigger on key down, so your hot key might still be held down when you call SendInput(). I normally use something like GetAsyncKeyState() or create a keyboard hook to check when the hotkey is released.

        S Offline
        S Offline
        sashok_bg
        wrote on last edited by
        #3

        Thank you for your reply. I am currently doing some reading on getting and setting the keyboard state. I think that you are right about the hotkey "being pressed" while I am trying to send the input. I will let you know as soon as I get the tests done with get/setKeyboardState. Thank you once again ! Best Regards, Alex

        1 Reply Last reply
        0
        • S sashok_bg

          Hello everyone, I was trying to make an application, that registers a hotkey and when the hotkey is pressed, simulates a keyboard event or mouse click. To do so, I have chosen to use the RegisterHotkey(), SendInput() and GetMessage() windows functions. The problem is that as soon as I declare a call to GetMessage() - SendInput() stops working. In the code below, I made an experiment - the program is waiting for 3 seconds (to give me some time) and as soon as I start it, I bring notepad to focus and the "KeyDown(97)" types "1" in the notepad window. However, this behavior does not work when the MessageLoop() is entered. Thank you for your help. Best Regards, Alex

          #define _WIN32_WINNT 0x0500

          #include "stdafx.h"
          #include //This function simulates a keyboard button press and release. Argument is VirtualKey
          //This function works fine, before the GetMessage() function is declared/called
          void KeyDown(int vk){

          printf("Called KeyDown with parameter %d \\n",vk);
          
          INPUT	Input\[1\];
          
          Input\[0\].type  = INPUT\_KEYBOARD;
          Input\[0\].ki.wVk= vk;
          
          printf("Sending Input\\n");
          SendInput(1, Input, sizeof(Input));
          

          }

          //This function is blocking and waiting for a message to arrive
          void MessageLoop(){
          MSG msg = {0};
          while (GetMessage(&msg, NULL, 0, 0) != 0)
          {

          	//if the arrived message is a hotkey event
              if (msg.message == WM\_HOTKEY)
              {
          		//some debugging
          		printf("msg.message %d, msg.lParam %d, msg.wParam %d \\n",msg.message,msg.lParam,msg.wParam);
          
          		//press the numeric '1' on the keyboard - doesn't work
          		KeyDown(97);
              }
          
          	TranslateMessage(&msg);
          	DispatchMessage(&msg);
          }
          

          }

          int main(int argc, char* argv[])
          {

          //This part works, only when the GetMessage() function is in a separate function ex: MessageLoop----
          Sleep(3000);
          KeyDown(97);
          
          //Register an ALT-CTR-A hotkey with ID - 1. First argument is NULL to make the hotkey global (it is a hWnd)
          RegisterHotKey(
          	NULL,
          	1,
          	MOD\_CONTROL | MOD\_ALT,
          	0x41
          );  //0x41 is 'a'
          
          //Enter the message loop that blocks and waits for a message to arrive for this application
          
          //Note that if I put the 'while (GetMessage(&msg, NULL, 0, 0) != 0)' code here, 
          //instead of putting it in a separate function, 
          //the 'KeyDown(97)' call on the 2nd line of the main() function will not work !
          MessageLoop();
          
          //clean up
          UnregisterHotKey(NULL,1);
          
          return 0;
          

          }

          J Offline
          J Offline
          JohnCz
          wrote on last edited by
          #4

          I know it is an old thread, but I think it is worth to explain the problem. You wrote console application. Console does not process any messages, hence your GetMessage (as you pointed) is blocking the thread since it waits for the message to come. There is no messages to handle and you are sitting in the GetMessage without ever exiting. Put a break point in the message loop and see if it is ever hit.

          JohnCz

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups