Search code examples
c#winapihookuikeyboardkeyboard-hook

Ignore mouse click and send keyboard Input to external application


How do I simulate keystrokes when mouse is clicked in a legacy program. NOTE! The program may not know that the mouse is clicked

I'm experimenting with

 IntPtr module = LoadLibrary("user32.dll");
 _mouseHookHandle = (HookType.MouseLowLevel, _mouseDelegate, module, 0);

and test to return -1 from the HookProc. But when I do so, SendInput don't send my input to the application. If I return the result from CallNextHookEx, SendInput works, but then the mouse click are sended to the legacy application to.

Background

We have a program that is controlled with a special keyboard. When pressing a key on the keyboard, it sends a sequence of ESC and letters. The program then performs an operation based on what the mouse is placed over the screen.

I am developing an on-screen keyboard so that you can control the application without this special keyboard. The user selects a button on-screen keyboard with the mouse. Then the user moves the mouse pointer to the object he wants to send the commando to, and then click again. But that said, mouse click may not be passed on to the program, the program performs another operation at mouse click.


Solution

  • As one of possible ways, you can use simple SendMessage to send any window messages directly to the target window. It can be used with windows of external application. (but you need to find the target window's handle to use this function). In this way you can send any keyboard and mouse events as well.

    Ignoring mouse events is more difficult. You need to subclass that window, i.e. to attach your own custom window procedure to filter window messages. To achieve this, you need to inject your DLL into the controlled process.
    The overall task is quite complicated, it's not a simple code snippet that you can copy-paste.