1

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

benjeen
  • 13
  • 1
  • 3
  • 1
    What are you hoping to achieve with this? What's the issue with the code you link to? – G42 Feb 14 '18 at 17:37
  • I am hoping to tweak the code for it to achieve steps 1 through 3 listed in my question above. Currently, the code I linked to is only doing step 1 – benjeen Feb 14 '18 at 17:59
  • Thanks. I mean what will you achieve once steps 1 to 3 are done; what's the final result of the click, drag, release? – G42 Feb 14 '18 at 18:06
  • maybe using [AutoIT](https://www.autoitscript.com/site/autoit/) for GUI automation is a better option – Loïc MICHEL Feb 14 '18 at 18:07
  • Good questions. You got it gms0ulman. I want to highlight a selection of data. I was going to use a Sendkeys function to "copy to clipboard" – benjeen Feb 14 '18 at 18:15
  • for a use-case like that I hav a quick-and-dirty (uses Java) solution: I run http://www.sikulix.com in scripts and then continue with PowerShell. – Guenther Schmitz Feb 14 '18 at 18:16
  • from which application do you want to select the data? a browser? excel? word? text-file? – Guenther Schmitz Feb 14 '18 at 18:16
  • sikulix seems very interesting. trying to avoid installing 3rd party applications so I cannot use AutoIT either. – benjeen Feb 14 '18 at 21:03

1 Answers1

4

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.

Palle Due
  • 5,929
  • 4
  • 17
  • 32