0

I write a code for mouse Click Simulation but there is one problem. It isn't making every second Click but cursor is moving. Can any one help me ? here is a code :

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

            [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool SetCursorPos(int X, int Y);

    private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
            private const int MOUSEEVENTF_MOVE = 0x0001;
            private const int MOUSEEVENT_LEFTDOWN = 0x0002;
            private const int MOUSEEVENTF_LEFTUP = 0x0004;

    private void button2_Click(object sender, EventArgs e)
            {

                SetCursorPos(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox7.Text), Convert.ToInt32(textBox8.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox7.Text), Convert.ToInt32(textBox8.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox9.Text), Convert.ToInt32(textBox10.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox9.Text), Convert.ToInt32(textBox10.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox11.Text), Convert.ToInt32(textBox12.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox11.Text), Convert.ToInt32(textBox12.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox13.Text), Convert.ToInt32(textBox14.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox13.Text), Convert.ToInt32(textBox14.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text), 0, 0);
                Thread.Sleep(1000);
                SetCursorPos(Convert.ToInt32(textBox17.Text), Convert.ToInt32(textBox18.Text));
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENT_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToInt32(textBox17.Text), Convert.ToInt32(textBox18.Text), 0, 0);
            }

Thats all.

dave_bulac
  • 45
  • 2
  • 8
  • can you use the debugger to step through the code? – MethodMan Sep 17 '15 at 18:26
  • 1
    take a look here maybe this nicely structured code can lead you to some better ideas http://stackoverflow.com/questions/18849421/how-to-move-the-cursor-or-simulate-clicks-for-other-applications – MethodMan Sep 17 '15 at 18:28
  • Use `mouse_event` for the motion as well as the clicks. `SetCursorPos` shouldn't be needed at all. – Ben Voigt Sep 17 '15 at 19:17

1 Answers1

1

The most likely issue is that you are setting both MOUSEEVENT_LEFTDOWN and MOUSEEVENTF_LEFTUP in a single mouse_event call. Try to simulate pressing the button down with a mouse_event call with MOUSEEVENT_LEFTDOWN set, then make a subsequent additional mouse_event call with MOUSEEVENTF_LEFTUP set to complete the click action (two calls per click).

D Coetzee
  • 789
  • 5
  • 5