1

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:

  • The purpose is to allow user input from a Xbox 360 controller (compatible with PC) to steer/control the computer that it is connected to.
  • The best solution that I have so far found is the "Windows.Forms.SendKeys" method but it only works for keyboard events.

Thanks in advance! :D

KingsNshit
  • 13
  • 5
  • You canuse the `PerformClick()` method if you just want to click in Buttons. [Button.PerformClick Method Documentation](http://msdn.microsoft.com/en-us/library/system.windows.forms.button.performclick.aspx) If you want just generic mouse clicks, you can see the following thread. http://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c – guanabara May 02 '13 at 12:51

1 Answers1

3

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);
    }
Felix Rabe
  • 48
  • 3
  • I tried mouse-event method but even though press and release can be separated the effect was the same: a press and release at the same time. – KingsNshit May 02 '13 at 13:01
  • 1
    You can separate the keydown and keyup like this: mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero); http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/dd375b80-775e-4c8b-ab25-2985f272ddec – Felix Rabe May 02 '13 at 13:10