Search code examples
powershelleventsclickmouse

How I can send mouse click in powershell?


How I can send mouse click on this position with this code. I want my mouse to go there and click.

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($xposi,$yposi)

Solution

  • If you don't feel like writing your own function, the Windows Automation Snapin for Powershell has a Send-Click() function. Alternatively you can import some functionality from the Windows API using mouse_event() although I would recommend SendInput() as it supersedes mouse_event(). Below is a fully functional sample that uses P/Invoke to get the functionality you want and send a left click to a specified screen coordinate:

    $cSource = @'
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    public class Clicker
    {
        // https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-input
        [StructLayout(LayoutKind.Sequential)]
        struct INPUT
        { 
            public int        type; // 0 = INPUT_MOUSE
                                    // 1 = INPUT_KEYBOARD
                                    // 2 = INPUT_HARDWARE
            public MOUSEINPUT mi;
        }
    
        // https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput
        [StructLayout(LayoutKind.Sequential)]
        struct MOUSEINPUT
        {
            public int    dx;
            public int    dy;
            public int    mouseData;
            public int    dwFlags;
            public int    time;
            public IntPtr dwExtraInfo;
        }
    
        // This covers most use cases although complex mice may have additional buttons.
        // There are additional constants you can use for those cases, see the MSDN page.
        const int MOUSEEVENTF_MOVE       = 0x0001;
        const int MOUSEEVENTF_LEFTDOWN   = 0x0002;
        const int MOUSEEVENTF_LEFTUP     = 0x0004;
        const int MOUSEEVENTF_RIGHTDOWN  = 0x0008;
        const int MOUSEEVENTF_RIGHTUP    = 0x0010;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP   = 0x0040;
        const int MOUSEEVENTF_WHEEL      = 0x0080;
        const int MOUSEEVENTF_XDOWN      = 0x0100;
        const int MOUSEEVENTF_XUP        = 0x0200;
        const int MOUSEEVENTF_ABSOLUTE   = 0x8000;
    
        const int screen_length = 0x10000;
    
        // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
    
        public static void LeftClickAtPoint(int x, int y)
        {
            // Move the mouse
            INPUT[] input = new INPUT[3];
    
            input[0].mi.dx = x * (65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
            input[0].mi.dy = y * (65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
            input[0].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
    
            // Left mouse button down
            input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    
            // Left mouse button up
            input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    
            SendInput(3, input, Marshal.SizeOf(input[0]));
        }
    }
    '@
    
    Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
    
    # Send a click at a specified point
    [Clicker]::LeftClickAtPoint(600, 600)