通过键盘消息发送数据 [英] send data through keyboard message

查看:133
本文介绍了通过键盘消息发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在dll中创建了一个全局键盘钩子,但是我发现向dll发送数据(在我的情况下为坐标)存在问题.我想到了通过在lParam中发送数据来使用键盘消息的想法.失败.我想知道为什么吗?代码如下.

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

在挂钩dll


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



似乎PostMessage(WM_KEYDOWN,VK_F7,400*(0x10000)+400)不起作用



It seems that PostMessage(WM_KEYDOWN,VK_F7,400*(0x10000)+400) does not work

推荐答案

更强大的方法是通过Windows API SendInput发送它,请参见http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx [ ^ ].此方法紧密模拟物理键盘.同时,它很容易正确使用.它不需要对消息参数进行棘手的计算(lParam尤其棘手,容易出错).

请尝试-效果很好.

—SA
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


我认为您没有正确使用PostMessage(),请参阅详细信息:
http://msdn.microsoft.com/en-us/library/ms644944% 28VS.85%29.aspx [ ^ ]

或MFC版本:
http://msdn.microsoft.com/en-us/library/9tdesxec% 28VS.80%29.aspx [ ^ ]

无论哪种情况,PostMessage都需要目标窗口句柄才能正常工作.
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.


这篇关于通过键盘消息发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆