您的位置:首页 > 运维架构 > Shell

power shell 操作键盘鼠标

2017-03-01 14:41 357 查看
# 操作键盘
$wshell = New-Object -ComObject wscript.shell
# 操作鼠标
function Click-MouseButton
{
param([string]$Button, [switch]$help)
$HelpInfo = @'

Function : Click-MouseButton
By       : John Bartels
Date     : 12/16/2012
Purpose  : Clicks the Specified Mouse Button
Usage    : Click-MouseButton [-Help][-Button x]
where
-Help         displays this help
-Button       specify the Button You Wish to Click {left, middle, right}

'@

if ($help -or (!$Button))
{
write-host $HelpInfo
return
}
else
{
$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);
'@

$SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru
if($Button -eq "left")
{
$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
}
if($Button -eq "right")
{
$SendMouseClick::mouse_event(0x00000008, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000010, 0, 0, 0, 0);
}
if($Button -eq "middle")
{
$SendMouseClick::mouse_event(0x00000020, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000040, 0, 0, 0, 0);
}
}
}

# 创建窗口
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object system.Windows.Forms.Form

# 显示对话框
$form.Text = "开始"
$form.ShowDialog()

# 休息5秒
Start-Sleep -Seconds 5

# 循环
for($index = 0;$index -le 100;$index++){
echo $index
$wshell.SendKeys("{END}")
Start-Sleep -Milliseconds 150
Click-MouseButton "left"
}

# 提示结束
$form.Text = "结束"
$form.ShowDialog()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  power-shel 鼠标 键盘