SendInput() does not work after GetMessage()
-
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;
}
-
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;
}
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.
-
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.
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
-
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;
}
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