Powershell using windows API to drag drop mouse

0

I have code that is supposed to do a drag and drop with powershell that I don't understand why it's not working the way I'm hoping for it to work. Details below:

function Mouse-signature-import(){
    $global:signature=@' 
      [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
      public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@ 

    $global:SendMouseClick = Add-Type -memberDefinition $global:signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru 
}

function Mouse-Drag($from1,$from2,$to1,$to2){
    Mouse-signature-import
    [System.Windows.Forms.Cursor]::Position = "$from1,$from2"
    $global:SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
    $global:SendMouseClick::mouse_event(0x80000000, 0, 0, 0, 0); Dont know if I need this because drag is not working in all apps
    [System.Windows.Forms.Cursor]::Position = "$to1,$to2"
    $global:SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
    write-host -f yellow -b black "Mouse-Drag" -nonewline; write-host -f Gray " from " -NoNewline; write-host -f Magenta "[" -nonewline; write-host -f Red "$from1 $from2" -nonewline;write-host -f Magenta "]" -nonewline; write-host -f Gray " to " -NoNewline; write-host -f Magenta "[" -nonewline; write-host -f Red "$to1 $to2" -nonewline; write-host -f Magenta "]";
}

I see the mouse moving and the dragging occurs, I'm using this to select text however. On Mouse-Up, the text becomes no longer highlighted. I need to adjust this code to prevent the mouse-up from unselecting the text I have highlighted in the drag motion. Ideally I will be using sendkeys after this function to do a "^c" to copy most of the time I use this function.

I added this line thinking it would help.

$global:SendMouseClick::mouse_event(0x80000000, 0, 0, 0, 0);

===================== SOLUTION:

It was the timing as DK suggested. I just addded a sleep before the mouse-up event.

function Mouse-Drag($from1,$from2,$to1,$to2){ $global:silentMouseFunctions = $true Mouse-signature-import

[System.Windows.Forms.Cursor]::Position = "$from1,$from2"
$global:SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
[System.Windows.Forms.Cursor]::Position = "$to1,$to2"
start-sleep -s 1 # If we do not sleep, then the drag does not work right.
$global:SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);

$global:silentMouseFunctions = $false
write-host -f yellow -b black "Mouse-Drag" -nonewline; write-host -f Gray " from " -NoNewline; write-host -f Magenta "[" -nonewline; write-host -f Red "$from1 $from2" -nonewline;write-host -f Magenta "]" -nonewline; write-host -f Gray " to " -NoNewline; write-host -f Magenta "[" -nonewline; write-host -f Red "$to1 $to2" -nonewline; write-host -f Magenta "]";
powershell
asked on Stack Overflow Feb 12, 2020 by shadow2020 • edited Feb 17, 2020 by shadow2020

1 Answer

1

After spending some time to debug the code, it seems that we need to slowly move the cursor for drag-and-drop to work correctly.

Below code is tested for several times for moving an icon on the desktop, it worked just fine on my PC:

Add-Type @"
    using System;
    using System.Runtime.InteropServices;

    public static class Win32 
    {
        [DllImport("user32.dll")]
        static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int x, int y);
    }
"@;

[Win32]::SetCursorPos(25, 25);

[Win32]::mouse_event(0x0002, 0, 0, 0, 0);

for ($i = 0; $i -lt 1000; $i+=100)
{
    [Win32]::SetCursorPos($i, $i);
    Start-Sleep -m 25
}

[Win32]::mouse_event(0x0004, 0, 0, 0, 0);
answered on Stack Overflow Feb 17, 2020 by DK Dhilip

User contributions licensed under CC BY-SA 3.0