C# Console App to Record and Playback Mouse Movements

Posted on 20th April 2020

Here is a simple C# console application that uses Win32 API function calls to record and playback mouse movements. The functions used in this program are from the Win32 API library user32.dll which contains functions to handle user interface. The functions in Win32 API libraries can be accessed from .Net using a functionality called P/Invoke or Platform Invocation Services. P/Invoke allows managed code written in C# or VB to call unmanaged functions in DLLs such as user32.dll.

Before we jump in, let me explain some of the key elements of this program.

  • P/Invoke is made possible in .NET through the System.Runtime.InteropServices namespace hence this namespace must be included.

    using System.Runtime.InteropServices;
  • GetCursorPos gets the current position of the mouse cursor. A variable of the type POINT is passed to this function and the coordinates are received in this variable.

    public static extern bool GetCursorPos(out POINT lpPoint);

    The structure POINT contains two members X and Y which contains the x and y coordinates of the cursor.

    GetCursorPos returns true of the cursor position can be retrieved or false otherwise.

  • SetCursorPos sets the position of the mouse cursor to the x and y coordinates that are passed to the function.

    public static extern bool SetCursorPos(int X, int Y);

    This function returns true if success and false if fails.

  • Recording mouse movements - This program repeatedly calls the GetCursorPos function to get the cursor positions. If the current position is different from the previous position, then the current coordinate position is added to a list and also displayed on the screen.

    The start and stop of the recording is controlled by the user pressing any key. The Console.KeyAvailable and Console.ReadKey functions are used for this purpose.

  • To playback the recorded movements, a foreach loop is used to iterate through the list of screen coordinates and each of these coordinates are passed to the SetCursorPos function.

    A small delay is added to slow down the playback motion.

    System.Threading.Thread.Sleep(50);

Here is the full program.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace RecordPlayMouseMove
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out POINT lpPoint);

        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);

        public struct POINT
        {
            public int X;
            public int Y;
        }

        static void Main(string[] args)
        {
            POINT current_pos, prev_pos;
            List<POINT> coords = new< List<POINT>();

            prev_pos.X = 0;
            prev_pos.Y = 0;


            Console.WriteLine("Press any key to start/stop recording mouse movements.");
            Console.ReadKey();
            do {  
                if (GetCursorPos(out current_pos)) {

                    if ((current_pos.X != prev_pos.X )|| (current_pos.Y != prev_pos.Y))
                    {

                        Console.WriteLine("({0},{1})", current_pos.X, current_pos.Y);
                        coords.Add(current_pos);
                    }

                    prev_pos.X = current_pos.X;
                    prev_pos.Y = current_pos.Y;
                }

            } while (!Console.KeyAvailable);
            Console.ReadKey();

            Console.WriteLine("Press any key to play the recorded mouse positions.");
            Console.ReadKey();
            foreach (POINT coord in coords)
            {
                SetCursorPos(coord.X, coord.Y);
                System.Threading.Thread.Sleep(50);
                if (Console.KeyAvailable) break;
            }
        }
    }
}

Post a comment

Comments

Nothing yet..be the first to share wisdom.