tags:

views:

274

answers:

1

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?

A: 

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.

Nick D
i tried this before. i still get two characters :(if i press key 'a' and the corresponding key i am sending is அ (U+0B8A).but i get 'அa' in the notepad
coder
@coder, see my update. I don't think it's the best solution but it might solve your problems.
Nick D
hey,It worked. the thing is i have to capture few thing in addition to including the lines u gave.i have to return 1 at the end of the callback function. to prevent characters being displayed, i returned CallNextHookEx in my if else statement which has the code for sendinput.
coder
would you mind posting what your solution looks like?
cksubs