0
0

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-02-28

概要

PowerShellを使用して、現在のマウスカーソルの座標を調べる方法を記載しました。

コマンド

以下のコマンドを入力してください。

入力

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Start-Sleep -s 5
$X = [System.Windows.Forms.Cursor]::Position.X
$Y = [System.Windows.Forms.Cursor]::Position.Y
Write-Output "X座標:$X | Y座標:$Y"

コマンドを実行して、Write-Output "X座標:$X | Y座標:$Y"の後にEnterキーを押すと、座標が表示されます。

解説

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  • Windows Formsの機能をPowerShellで使用できるようにしています
  • System.Windows.Formsにマウスカーソルの位置を取得する機能が含まれています
  • [void]は戻り値を無視するための指定です
Start-Sleep -s 5
  • -sパラメータは秒単位での指定を意味します
  • 5秒間の待機します
  • この時間を使用して、測定したい位置にマウスカーソルを移動できます
$X = [System.Windows.Forms.Cursor]::Position.X
$Y = [System.Windows.Forms.Cursor]::Position.Y
  • 変数名の先頭に$を付けることで、変数であることを示すことができます
  • [System.Windows.Forms.Cursor]::Positionで現在のマウスカーソルの位置を取得できます
  • X座標は画面左端からの水平距離を表します
  • Y座標は画面上端からの垂直距離を表します

座標は画面左上を原点(0,0)としているイメージです。

Write-Output "X座標:$X | Y座標:$Y"
  • Write-Outputは出力コマンドのひとつです。ここでは取得した座標を出力しています

終わりに

マウスポインタの座標は画面の解像度によって変化するのでそこは要注意です。

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?