2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

■ PowerShellで自動マウス操作 ~多分これが一番簡単だと思います~

Last updated at Posted at 2025-05-14

PowerShell_banner.png

こんにちは★
好奇心過多なコーダー 零壱(ゼロイチ)テクトです。

僕の所属する会社のPCでは、10分でスクリーンセーバが動いてしまうため
複数のPCで作業していると、すぐにPCがパス入力画面になってしまいます。

その解除が面倒で、以前VBSでマウスを自動で動かすマクロを作った事がありますが
VBSが廃止になるという事(いつだろう)で、今更勉強でPowerShellで組んでみました。

先人の100番煎じですが、思いのほかコンパクトに作れたので公開させて下さい。

1.概要/仕様

・マウスを指定時間ごとに一定に動かす
・業務効率を考えて動くマウスは1px(操作の邪魔しない
・極力シンプルに単機能に
・Ctrl+Cでコードを止めたい

2.コード

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

public class MouseJiggler {
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, IntPtr dwExtraInfo);
    public const uint MOUSEEVENTF_MOVE = 0x0001;
}
"@

$stoptimeSec = 5 # xx秒ごとに動作
write-host "CTRL + C で止まります"

# ▼ループ (CTRL + C で停止)
while ($true) {
    # ▼1pxマウス動かす
    [MouseJiggler]::mouse_event(0x0001, 1, 0, 0, [IntPtr]::Zero)
    Start-Sleep -Milliseconds 100
    [MouseJiggler]::mouse_event(0x0001, -1, 0, 0, [IntPtr]::Zero)

    # ▼指定時間待機
    Start-Sleep -Seconds $stoptimeSec
}

3.備考

・特に大したものではいので、使いたい方はどうぞお使いください。

・ポイントはマウスを指定の場所に移動したりせず
 現行のマウスカーソル位置から1pxだけずらして戻るため
 コードを実行させながら
 PCを触っていても触っていなくても影響がない事です。


           どこかの誰かの何かの足しになれば幸いです。
                          01000010 01011001 01000101

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?