4
votes

I have a win 32 application written in c++ which sets the low level keyboard hook. now i want to sendInput to any app like word / notepad. how do i do this?

i have already done enough of using 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. the problem is i get a call to my callback function with the pressed key.

for e.g i pressed A and i want to send U+0BAF unicode character to the active applciation windows. in this case, assume it is notepad.

the problem is i get two characters U+0BAF and A in the notepad.

A is being sent because i am calling CallNextHookEx( NULL, nCode, wParam, lParam);

if i return 1 after sendInput, then nothing is sent to notepad.

any suggestion?

1

1 Answers

4
votes

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 & 0x10)) {
    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.

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

ie, #define WM_MY_KEYDOWN (WM_USER + 1)

and call the appropriate routine that will generate new key events.