3

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.

magol
  • 6,135
  • 17
  • 65
  • 120

3 Answers3

2

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.

user626528
  • 13,999
  • 30
  • 78
  • 146
  • I solved it by creating a transparent window that I put over the screen. It takes care of mouse click, and then I send the SendMessage to the window that is under the mouse – magol Dec 28 '12 at 14:35
0

If I understood what you are after then you can SendKeys by code on MouseClick event or by some script using System.Windows.Form.SendKeys -> Send({KeyBoardKey})

Documentation : http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • I'm using http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx to send input. But it is not the problem. The problem is to send input after I ignoring the mouse click that I receive via low level mouse hook. – magol May 23 '12 at 21:01
0

If your on screen keyboard application is a separate process to the legacy application then it should be fairly easy.

In your keyboard app you know which key the user has clicked. So then all you have to do is pass this info to the legacy app using SendMessage() API.

You say the physical keyboard sends ESC and letters? So you'll need some sort of look up table of keyboard keys to command sequences.

Also you will need to get the HWND of the legacy application which you can do by using the FindWindow()/FindWindowEx() API's. I assume the app will have a unique custom class name which should make this easy.

Details of how to send the command sequences chars using SendMessage() here:

Sending an application keystrokes with "SendMessage" (vb.net)

Community
  • 1
  • 1
paulm
  • 5,629
  • 7
  • 47
  • 70