如何在powershell中发送鼠标点击 [英] How i can send mouse click in powershell
问题描述
[p]我可以用这个代码发送鼠标点击这个位置, System.Windows.Forms.Cursor] :: Position = New-Object System.Drawing.Point($ xposi,$ yposi)
感谢您的答案。
如果您不想编写自己的功能, Windows自动化Snapin for Powershell 具有发送点击功能。或者,您可以从导入一些功能Windows API使用mouse_event 虽然我建议 SendInput ,因为它取代了mouse_event。以下是使用P / Invoke获取所需功能的全功能示例,并将左键单击发送到指定的屏幕坐标。
$ cSource = @'
using System;
使用System.Drawing;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
public class Clicker
{
//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;
}
//这涵盖了大多数用例,虽然复杂的鼠标可能有额外的按钮
//还有其他常量可以用于这些情况,请参阅msdn页面
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 LeftClickAtPoint(int x,int y)
{
//移动鼠标
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_MOVED | MOUSEEVENTF_ABSOLUTE;
//鼠标左键
输入[1] .mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
//鼠标左键向上
输入[2] .mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(3,input,Marshal.SizeOf(input [0]));
}
}
'@
添加类型-TypeDefinition $ cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
#在指定点发送点击
[Clicker] :: LeftClickAtPoint(600,600)
How i can send mouse click on this position with this code.i want my mouse go there and click.
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($xposi,$yposi)
thanks for answers.
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://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 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_MOVED | 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)
这篇关于如何在powershell中发送鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!