6

Here is the original question but it is considered to java: Simulate mouse clicks at a certain position on inactive window in Java?

Anyways, I'm building a bot to run in the background. This bot requires me to click. Of course, I want to be able to do other things while the bot is running.

So I was wondering if it was possible for me to simulate a mouse click at a certain position on an inactive window.

If this is possible, I would greatly appreciate it if any of you could help me.

Thanks!

Community
  • 1
  • 1
Ritam Nemira
  • 4,051
  • 4
  • 26
  • 21
  • Ideally you should use `SendInput`, but that requires input focus. Otherwise you'll have to try your luck with `PostMessage`. Why don't you use UIAutomation rather than faking input? – David Heffernan Nov 23 '11 at 12:48
  • This answer is just an idea... @DavidHeffernan you can think in rembeber the global coords of the mouse position and restore it after the action, so also the focus. Ok, you will loose the focus but its for a milisecond. But this method can change the status of a full screen view, like a game or something like. I think it's a good option do a little window with property "always on top" and then do it there. If it's for a web, for eg, you can scroll it automatically with a web browser and then click what you want. – Leandro Bardelli Nov 23 '11 at 13:11

1 Answers1

4

Yes it is possible, here's the code I used for a previous school project:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

//This simulates a left mouse click
public static void LeftMouseClick(Point position)
{
    Cursor.Position = position;
    mouse_event(MOUSEEVENTF_LEFTDOWN, position.X, position.Y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, position.X, position.Y, 0, 0);
}

EDIT : It seems that the mouse_event function was replaced by SendInput() but it still works (Windows 7 and earlier)

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • 1
    That requires the target window to have the input focus. – David Heffernan Nov 23 '11 at 12:55
  • 2
    `mouse_event` is perfectly fine, it's equivalent to `SendInput` but much easier to drive. – David Heffernan Nov 23 '11 at 12:58
  • @DavidHeffernan I'm sorry I misread his question, but is that even possible to click without being active ? Also, do you happen to know why this function is being replaced ? I didn't find that specific info on the MSDN page. – Nasreddine Nov 23 '11 at 13:03
  • 3
    `SendInput` is a more general flexible function. `mouse_event` and `keybd_event` are retained for compatibility with old software. I see no reason to stop using `mouse_event`. You can post Windows messages to an inactive Windows and sometimes succeed in faking input. However, I would look to UIautomation before faking input. – David Heffernan Nov 23 '11 at 13:10