SendInput and GetMessage do not work together.

Hello Everyone,

I have been trying to make a simple C++ program that combines the use of Hotkeys and Generating Keyboard Input.

I have however noted that my "SendInput" function stops working whenever I declare the GetMessage function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#define _WIN32_WINNT 0x0500

#include "stdafx.h"
#include <Windows.h>

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));
}

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

        //Try to send input, before we go to the "Message Loop" - this fails
	Sleep(3000);
	KeyDown(97);


	RegisterHotKey(
		NULL,
		1,
		MOD_CONTROL | MOD_ALT,
		0x41
	);  //0x41 is 'a'


        //Message loop	
	MSG msg = {0};
	while (GetMessage(&msg, NULL, 0, 0) != 0)
        {
           if (msg.message == WM_HOTKEY)
           {
			printf("msg.message %d, msg.lParam %d, msg.wParam %d \n",msg.message,msg.lParam,msg.wParam);
			KeyDown(97);
           }
	   TranslateMessage(&msg);
	   DispatchMessage(&msg);
        }

	UnregisterHotKey(NULL,1);

	return 0;
}


If I get rid of the "GetMessage" function call, the first KeyDown (after the sleep) works perfectly, in the moment put the "GetMessage" function in the code, it stops working.

Please help.

Best Regards,

Alex K.
Please see the "Return" section of the article on the "GetMessage()" function from MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644936(v=vs.85).aspx

Specifically this part"
Because the return value can be nonzero, zero, or -1, avoid code like this:
while (GetMessage( lpMsg, hWnd, 0, 0))...
Hello and thank you for your reply. I read what you have advised me to read, but the problem persists.

Here is the new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include "stdafx.h"
#pragma comment( lib, "user32.lib" )

#include <windows.h>
#include <stdio.h>

void PressKey(int vk) {
    printf("sending input\n");

    KEYBDINPUT  kb = {0};
    INPUT       Input = {0};

    kb.wVk  = vk;
    Input.type  = INPUT_KEYBOARD;
    Input.ki  = kb;

	SendInput(1, &Input, sizeof(Input));

	printf("ERRORS: %d\n",GetLastError());
}

void MessageLoop()
{

	BOOL bRet;

	MSG msg = {0};
	while ( (bRet = GetMessage(&msg, NULL,WM_HOTKEY, WM_HOTKEY)) != 0)
	{

		if(bRet== -1){
			printf("ERROR %d\n",GetLastError());
		}
		else{
			if (msg.message == WM_HOTKEY)
			{
				printf("msg.message %d, msg.lParam %d, msg.wParam %d \n",msg.message,msg.lParam,msg.wParam);
				PressKey(98);
			}
		}	
	}

	printf("message loop ended\n");
}



int _tmain(int argc, _TCHAR* argv[])
{

	Sleep(2000);
        //this works
	PressKey(97);

	RegisterHotKey(NULL,1,MOD_CONTROL | MOD_ALT,0x41);  //0x41 is 'a'
	RegisterHotKey(NULL,2,MOD_CONTROL | MOD_ALT,0x5a);  //0x5a is 'z'
	
	MessageLoop();
	
       //this does not work
	PressKey(99);

	UnregisterHotKey(NULL,1);
	UnregisterHotKey(NULL,2);

	return 0;
}


I now have "while ( (bRet = GetMessage(&msg, NULL,WM_HOTKEY, WM_HOTKEY)) != 0)". But the same situation as before. Once the "GetMessage" function is called, the SendInput function does not work.

Any other ideas :( ?
Topic archived. No new replies allowed.