0

I need to press mouse left button in code.
I move the mouse using:

[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);

But I need a native (or not) method to perform a click in left button of the mouse.

It's a wpf application, however, when the mouse button is supposed to be pressed by the application, the mouse will be outside the wpf window (and the window will not have focus).

I ran into some codes like this:

MouseButtonEventArgs arg = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) {
    RoutedEvent = Button.MouseLeftButtonDownEvent
};

and then:

RaiseEvent(arg);

-or-

OnMouseLeftButtonDown(arg);

But they don't work. And I think it because these methods came from UIElement.

Maybe a dll import will do. I just don't know which.

  • 1
    It's unclear what you are trying to do and there is no sign of a question in your opening post. Can you restructure everything so we have an idea on a) what the goal is b) what you tried and c) where your problem is? Reading [ask] and [mcve] might help you there. – Milster Oct 10 '18 at 20:04
  • Possible duplicate of [How do you simulate Mouse Click in C#?](https://stackoverflow.com/questions/2416748/how-do-you-simulate-mouse-click-in-c) – vesan Oct 11 '18 at 04:52

1 Answers1

0

You need a gloabl mouse hook: Global Mousehook in Wpf

To simulate a mouse click you can use a nuget package called Inputsimulator.

juliushuck
  • 1,398
  • 1
  • 11
  • 25