NO IMAGE

マウスを自動で交互に動かすマクロ

# 終了方法:Ctrl+Cを押す、または、[×]ボタンを押す

# System.Windows.Formsをロード(.NETのCursorクラスを利用のため)
add-type -AssemblyName System.Windows.Forms

# mouse_event APIを利用準備
$sgnt=@’
[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);
‘@

$SendME = Add-Type -memberDefinition $sgnt -name “Win32MouseEventNew” -namespace Win32Functions -passThru

echo “Ctrl+Cで終了”

# マウス移動
$ME_MOVE = 0x00000001

# スリープ秒数(100秒)
$SleepSec = 10

# ・マウス移動イベントの振幅
$MMDist = 5
# ・マウスの座標を左右へ動かす幅
$MMDistX = 5

# 偶数回目は左、奇数回目は右へずらすフラグ
$Flg = $true

# 永久ループ(Ctrl+Cで割込み終了)
while ($true) {
# スリープさせる
Start-Sleep $SleepSec

# マウスのX,Y座標取得
$x = [System.Windows.Forms.Cursor]::Position.X
$y = [System.Windows.Forms.Cursor]::Position.Y

# マウスを左に移動
$SendME::mouse_event($ME_MOVE, -$MMDist, 0, 0, 0)

# マウスを右に移動
$SendME::mouse_event($ME_MOVE, $MMDist, 0, 0, 0)

# マウスの移動決定フラグ(左か右)
if ($Flg) {
$x += $MMDistX
$Flg = $false;
}
else {
$x -= $MMDistX
$Flg = $true
}
[System.Windows.Forms.Cursor]::Position = new-object System.Drawing.Point($x, $y)
$x = [System.Windows.Forms.Cursor]::Position.X
$y = [System.Windows.Forms.Cursor]::Position.Y