benjeen
benjeen

Reputation: 13

Powershell Function to left mouse click and drag to new screen coordinates

I am looking to have a Powershell function that does the following:

  1. Mouse Left Clicks and stays clicked at screen coordinates (x, y)
  2. Mouse Moves, while left click button is held, to new screen coordinates (x+800, y+1000)
  3. Mouse Left Click Releases at new screen coordinates

I am working with the code found in the post below (Posted by StephenP) but am open to other methods of achieving this automation task:

How i can send mouse click in powershell

Upvotes: 1

Views: 5958

Answers (1)

Palle Due
Palle Due

Reputation: 6292

Building on the code in your referenced question, you can do this:

$cSource = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Dragger
{
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{ 
    public int        type; // 0 = INPUT_MOUSE,
                            // 1 = INPUT_KEYBOARD
                            // 2 = INPUT_HARDWARE
    public MOUSEINPUT mi;
}

//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx
[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_MOVED      = 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://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

public static void Drag(int x1, int y1, int x2, int y2)
{
    //Move the mouse
    INPUT[] input = new INPUT[4];
    input[0].mi.dx = x1*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
    input[0].mi.dy = y1*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
    input[0].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
    //Left mouse button down
    input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    // Mouse move
    input[2].mi.dx = x2*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
    input[2].mi.dy = y2*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
    input[2].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
    //Left mouse button up
    input[3].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(4, input, Marshal.SizeOf(input[0]));
}
}
'@
Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
#Send drag from one point to another
[Dragger]::Drag(50, 20, 200, 100)

What you want is not a left click, but a left mouse down, followed by a mouse move and a mouse up. The example above drags a window (provided there is one) from 50,20 to 200,100. Just enter your own coordinates and you should be good to go.

Upvotes: 4

Related Questions