简体   繁体   English

进行 SendInput 时调用全局低级键盘挂钩。 如何预防?

[英]global low level keyboard hook being called when SendInput is made. how to prevent it?

I have a win 32 application written in c++ which sets the low level keyboard hook.我有一个用 c++ 编写的 win 32 应用程序,它设置了低级键盘钩子。 now i want to sendInput to any app like word / notepad.现在我想将输入发送到任何应用程序,如 word/记事本。 how do i do this?我该怎么做呢?

i have already done enough of using findwindow / sendmessage.我已经使用 findwindow / sendmessage 做的够多了。 for all these, i need to know edit controls.对于所有这些,我需要知道编辑控件。 finding the edit control is very difficult.找到编辑控件非常困难。

since SendInput works for any windows application, i want to use it.由于 SendInput 适用于任何 Windows 应用程序,我想使用它。 the problem is i get a call to my callback function with the pressed key.问题是我用按下的键调用了我的回调函数。

for eg i pressed A and i want to send U+0BAF unicode character to the active applciation windows.例如,我按下 A 并且我想将 U+0BAF unicode 字符发送到活动的应用程序窗口。 in this case, assume it is notepad.在这种情况下,假设它是记事本。

the problem is i get two characters U+0BAF and A in the notepad.问题是我在记事本中得到了两个字符 U+0BAF 和 A。

A is being sent because i am calling CallNextHookEx( NULL, nCode, wParam, lParam); A 正在发送,因为我正在调用 CallNextHookEx(NULL, nCode, wParam, lParam);

if i return 1 after sendInput, then nothing is sent to notepad.如果我在 sendInput 之后返回 1,则不会向记事本发送任何内容。

any suggestion?有什么建议吗?

If I understood your problem correctly, you should ignore "injected" key events in your hook procedure, like this:如果我正确理解了您的问题,您应该忽略挂钩过程中的“注入”关键事件,如下所示:

LRESULT CALLBACK
hook_proc( int code, WPARAM wParam, LPARAM lParam )
{
  KBDLLHOOKSTRUCT*  kbd = (KBDLLHOOKSTRUCT*)lParam;

  // Ignore injected events
  if (code < 0 || (kbd->flags & LLKHF_INJECTED)) {
    return CallNextHookEx(kbdhook, code, wParam, lParam);
  }
  ...

Update: additionally, you have to eat characters and notify some other routine for a character press through Windows messages.更新:此外,你必须吃掉字符并通过 Windows 消息通知字符按下的其他例程。

Example:例子:

...
// Pseudocode
if (kbd->vkCode is character) {
  if (WM_KEYDOWN == wParam) {
    PostMessage(mainwnd, WM_MY_KEYDOWN, kbd->vkCode, 0);
    return 1; // eat the char, ie 'a'
  }
}
return CallNextHookEx(kbdhook, code, wParam, lParam);

And, in some other module, you handle WM_MY_KEYDOWN:而且,在其他一些模块中,您处理 WM_MY_KEYDOWN:

ie, #define WM_MY_KEYDOWN (WM_USER + 1)

and call the appropriate routine that will generate new key events.并调用将生成新键事件的适当例程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM