powershell - Powershell使用windows API拖拽鼠标
问题描述
我的代码应该使用 powershell 进行拖放,但我不明白为什么它不能按照我希望的方式工作。详情如下:
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 "]";
}
我看到鼠标移动并且发生拖动,但是我正在使用它来选择文本。在 Mouse-Up 上,文本不再突出显示。我需要调整此代码以防止鼠标向上取消选择我在拖动动作中突出显示的文本。理想情况下,我将在这个函数之后使用 sendkeys 来执行“^c”来复制我大部分时间使用这个函数。
我添加了这一行,认为它会有所帮助。
$global:SendMouseClick::mouse_event(0x80000000, 0, 0, 0, 0);
====================== 解决方案:
这是 DK 建议的时机。我只是在鼠标向上事件之前添加了一个睡眠。
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 "]";
解决方案
在花了一些时间调试代码之后,似乎我们需要慢慢移动光标才能使拖放正常工作。
下面的代码经过多次测试以在桌面上移动图标,它在我的 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);
推荐阅读
- python - 使用python进行网页抓取,如何处理ngif?
- excel - 每次一个字符串范围包含另一个字符串范围的子集时,如何在 Excel 中计数?
- r - Javascript点击在Rselenium中不起作用
- asp.net-core - ASP.net Core 2.2 配置
- excel - 将多个工作表从一个工作簿保存到单个文件夹
- ssl - IIS 10 .NET CORE SSL 问题
- php - 如何从表中删除重复值
- c# - 分组方式包括(内部连接)
- javascript - 等待的承诺仍然返回
- google-app-engine - 使用 Google App Engine 和 Google Cloud SQL 的连接池