Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I create a global keyboard hook in dll.But I find that there is a problem sending data(coordinate in my case) to the dll.I come with the idea of using keyboard message by sending the data in lParam.But I failed.I wonder why?the code is as following.

PostMessage(WM_KEYDOWN,VK_F7,400*(0x10000)+400); // I use F7

in the hook dll


LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
	if (VK_F9==wParam)
	{
		SendMessage(g_hWnd,WM_CLOSE,0,0);
		StopKeyboardHook();
		return -1;
	}
	else if (VK_F6==wParam&&(0==(lParam>>30)&1))
	{
		if (htargetwnd)
		{
			//PostMessageA(htargetwnd, WM_LBUTTONDOWN, 0,400*(0x10000)+400);
			//PostMessageA(htargetwnd, WM_LBUTTONUP, 0,400*(0x10000)+400);
			PostMessageA(g_hWnd, WM_GETCOORDMESSAGE, 0,0);
			return -1;
		}
	}
	else if (VK_F7==wParam)//&&(0==(lParam>>30)&1)
	{
		if (htargetwnd)
		{
			PostMessageA(htargetwnd, WM_LBUTTONDOWN, 0,400*(0x10000)+400);
			return -1;
		}
	}
	return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
}


It seems that PostMessage(WM_KEYDOWN,VK_F7,400*(0x10000)+400) does not work
Posted
Updated 25-Apr-11 17:29pm
v2
Comments
Tarakeshwar Reddy 25-Apr-11 23:30pm    
Added pre tags

Much more robust way is sending it via Windows API SendInput, see http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx[^]. This method closely simulates physical keyboard. At the same time, it is much easy to use correctly; it does not require tricky calculation of message parameters (lParam is especially tricky, easy to make a mistake).

Please try — it works well.

—SA
 
Share this answer
 
I don't think you're using PostMessage() correctly, see details:
http://msdn.microsoft.com/en-us/library/ms644944%28VS.85%29.aspx[^]

Or the MFC version:
http://msdn.microsoft.com/en-us/library/9tdesxec%28VS.80%29.aspx[^]

In either case, PostMessage requires a target window handle to work properly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900