KingsNshit
KingsNshit

Reputation: 13

Simulating mouse events? C#

I'm trying to create a windows forms program which makes the computer act as if I pressed a mouse-button. I want to control the events manually (timing is not decided in advance) and it needs to possible to press and hold, so the mouse-button release should be a separate event.

The following information should not change the code, just help you further understand my situation:

Thanks in advance! :D

Upvotes: 1

Views: 2308

Answers (1)

Felix Rabe
Felix Rabe

Reputation: 48

I simulate mouse events like this

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint 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;

usage example:

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

Upvotes: 3

Related Questions