SoFunction
Updated on 2025-04-29

How to get mouse coordinates and simulate mouse clicks in real time in C#

C# gets mouse coordinates and simulates mouse clicks in real time

In C# programming, it is very common to obtain the mouse's coordinates on the screen in real time and simulate the mouse click operation.

These features can be used in automated testing, game aids, and other applications that need to interact with the user interface.

This article will introduce how to implement these two functions using C#.

1. Get mouse coordinates in real time

To get the mouse's coordinates on the screen in real time, you can use the `` class. This class provides a way to get the mouse position.

We can get the mouse coordinates in real time through the following code:

        public MainForm()
        {
            InitializeComponent();
            // Set the timer and update the mouse coordinates every 100 milliseconds            Timer timer = new Timer();
             = 100;
             += Timer_Tick;
            ();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            // Get the current mouse position            Point mousePosition = ;
            // Show coordinates on the form             = $"Mouse coordinates: X={}, Y={}";
        }

In this code, we use a `Timer` control, which updates the mouse's coordinates every 100 milliseconds, and displays the coordinates on the form's title bar.

2. Simulate mouse clicks

To simulate a mouse click operation, we can use the mouse_event function in the class and the ``mouse_event function.

The `mouse_event` function is part of the Windows API and can be used to simulate mouse events.

We need to import this function first:

        // Import mouse_event function        [DllImport("", CharSet = , CallingConvention = )]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

        // Mouse event constant        private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
        private const uint MOUSEEVENTF_LEFTUP = 0x04;
        private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const uint MOUSEEVENTF_RIGHTUP = 0x10;


        private void SimulateLeftClick(int x, int y)
        {
            // Move the mouse to the specified position             = new Point(x, y);
            // Simulate the left mouse button press and release            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)x, (uint)y, 0, 0);
        }

        private void SimulateRightClick(int x, int y)
        {
            // Move the mouse to the specified position             = new Point(x, y);
            // Simulate the right mouse button press and release            mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, (uint)x, (uint)y, 0, 0);
        }

        private void btnLeftClick_Click(object sender, EventArgs e)
        {
            // Simulate left-click at coordinates (100, 100)            SimulateLeftClick(100, 100);
        }

        private void btnRightClick_Click(object sender, EventArgs e)
        {
            // Simulate right click at coordinates (200, 200)            SimulateRightClick(200, 200);
        }
  • In this code, we first import the `mouse_event` function and define some constants to represent the mouse event.
  • We then created two methods `SimulateLeftClick` and `SimulateRightClick` to simulate left and right clicks respectively.
  • Finally, we call these methods in the button click event to simulate mouse clicks.

Notice:

When using the simulated mouse click function, make sure that your operations will not adversely affect the system or other applications.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.