0

I am trying to programatically simulate a left button mouse click:

        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int x, int y);

        [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 static void LeftMouseClick(int xpos, int ypos)
        {
            SetCursorPos(xpos, ypos);
            mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
            System.Threading.Thread.Sleep(1000);
            mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
        }

The cursor moves to the appropriate position on the screen, but the click is not firing there. Any thoughts?

EDIT: I did a couple of tests. The method i use does not seem to be the issue (it clicks on applications like steam, skype successfully). When calling the click method when the cursor is above the application that i specifically want to click on (an android emulator) nothing happens. The mouse cursor moves to the spot but wont click... Going to test another emulator now.

binbin
  • 25
  • 1
  • 6
  • Is it possible you aren't supposed to send an `OR`d event? I would think the up and down would happen at different times – BradleyDotNET Mar 25 '17 at 00:06
  • @BradleyDotNET i edited that to different events, its not firing still. – binbin Mar 25 '17 at 00:14
  • The event is injected into the queue of the thread with the focus. Which program is that? Faking input is likely not the solution to your actual problem. Even if it was you are meant to do it with SendInput. Didn't you read the documentation? – David Heffernan Mar 25 '17 at 06:37
  • @DavidHeffernan I think you are on to something. The program with focus is my application, not the application i want to click on. So i need the set the focus to the application i want to click on beforehand? – binbin Mar 26 '17 at 13:53

1 Answers1

0

I wrote a helper class for another project I was working on which wrapped up the mouse event:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

class MouseHelper
{
    internal struct INPUT
    {
        public UInt32 Type;
        public MOUSEKEYBDHARDWAREINPUT Data;
    }
    [StructLayout(LayoutKind.Explicit)]
    internal struct MOUSEKEYBDHARDWAREINPUT
    {
        [FieldOffset(0)]
        public MOUSEINPUT Mouse;
    }

    internal struct MOUSEINPUT
    {
        public Int32 X;
        public Int32 Y;
        public UInt32 MouseData;
        public UInt32 Flags;
        public UInt32 Time;
        public IntPtr ExtraInfo;
    }

    /// <summary>
    /// Synthesizes keystrokes, mouse motions, and button clicks.
    /// </summary>
    [DllImport("user32.dll")]
    internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

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

    public static void DoMouseClick()
    {
        var inputMouseDown = new INPUT();
        inputMouseDown.Type = 0; /// input type mouse
        inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down

        var inputMouseUp = new INPUT();
        inputMouseUp.Type = 0; /// input type mouse
        inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up

        var inputs = new INPUT[] { inputMouseDown, inputMouseUp };
        SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
    }

    public static void DoMouseClick2()
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
}

All I needed to do was call MouseHelper.DoMouseClick() whenever I wanted a mouse click. I suspect the mouse move functionality could be added as an enhancement.

Forty3
  • 2,199
  • 1
  • 14
  • 19
  • I tried your class, thank you. It appears to be working fine actually, the issue is that the click wont fire on the specific application i want to click on (an android emulator), everything else works fine – binbin Mar 26 '17 at 16:19
  • @binbin did you figure it out? – metoyou Feb 06 '21 at 10:30