如何模拟对任何当前聚焦的窗口的按键操作?
我正在尝试更改键盘发送到应用程序的键。 我已经创建了一个全局挂钩,并且可以阻止我想要的密钥,但我现在想发送一个新的密钥。 这是我的钩子过程:
LRESULT __declspec (dllexport) HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int ret;
if(nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
kbStruct = (KBDLLHOOKSTRUCT*)lParam;
printf("\nCaught [%x]", kbStruct->vkCode);
if(kbStruct->vkCode == VK_OEM_MINUS)
{
printf(" - oem minus!");
keybd_event(VK_DOWN, 72, KEYEVENTF_KEYUP, NULL);
return -1;
}
else if(kbStruct->vkCode == VK_OEM_PLUS)
{
printf(" - oem plus!");
keybd_event(VK_UP, 75, KEYEVENTF_KEYUP, NULL);
return -1;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
我尝试将 SendMessage 和 PostMessage 与 GetFocus() 和 GetForegroudWindow() 一起使用,但无法弄清楚如何为 WM_KEYUP 或 WM_KEYDOWN 创建 LPARAM。 我还尝试了 keybd_event(),它确实模拟了按键(我知道是因为这个钩子过程捕获了模拟的按键),包括 5 或 6 个不同的扫描代码,但没有任何影响我的前台窗口。
我基本上试图将我的 ms3200 上的缩放栏变成滚动控件,因此我什至可能发送了错误的键(向上和向下)。
I am trying to change the keys my keyboard sends to applications. I've already created a global hook and can prevent the keys I want, but I want to now send a new key in place. Here's my hook proc:
LRESULT __declspec (dllexport) HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int ret;
if(nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
kbStruct = (KBDLLHOOKSTRUCT*)lParam;
printf("\nCaught [%x]", kbStruct->vkCode);
if(kbStruct->vkCode == VK_OEM_MINUS)
{
printf(" - oem minus!");
keybd_event(VK_DOWN, 72, KEYEVENTF_KEYUP, NULL);
return -1;
}
else if(kbStruct->vkCode == VK_OEM_PLUS)
{
printf(" - oem plus!");
keybd_event(VK_UP, 75, KEYEVENTF_KEYUP, NULL);
return -1;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
I've tried using SendMessage and PostMessage with GetFocus() and GetForegroudWindow(), but can't figure out how to create the LPARAM for WM_KEYUP or WM_KEYDOWN. I also tried keybd_event(), which does simulate the keys (I know because this hook proc catches the simulated key presses), including 5 or 6 different scan codes, but nothing affects my foreground window.
I am basically trying to turn the zoom bar on my ms3200 into a scroll control, so I may even be sending the wrong keys (UP and DOWN).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用 keybd_event 是正确的。 如果您所做的只是按下按键,则窗口可能会处理按键按下消息。 您确实需要先按下按键,然后按下按键:
或者,更好的是,当 OEM 按键按下时发送按键按下,当 OEM 按键按下时发送按键抬起。 您可以通过 kbStruct->flags & 来判断 down/up 状态。 LLKHF_UP。
Calling keybd_event is correct. If all you're doing is a key up, maybe the window processes the key down message instead. You really need to send a key down followed by a key up:
Or, better yet, send the key down when the OEM key goes down and a key up when the OEM key goes up. You can tell the down/up state by kbStruct->flags & LLKHF_UP.
您可能希望使用 SendInput,如 < a href="http://msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx" rel="nofollow noreferrer">keybd_event 已被取代。 MSDN 杂志文章 C++ 问答:向任何应用程序发送击键有一个有用的例子。
You may wish to use SendInput, as keybd_event as has been superseded. The MSDN Magazine article C++ Q&A: Sending Keystrokes to Any App has a useful example.
您可能想尝试使用 Control-UpArrow 和 Control-DownArrow 而不是向上和向下。 然而,这似乎并不适用于所有应用程序,即使在它确实有效的应用程序上,它也可能取决于焦点在哪里。
You might want to try Control-UpArrow and Control-DownArrow instead of Up and Down. However this doesn't seem to work for all applications, and even on application where it does work, it may depend on where the focus is.