问题
I have been searching for this for a long time, and can't find one single answer.
Is it possible to check if my mouse or keyboard is in use (the mouse is currently moving or keys are being pressed) using only a Batch
or PowerShell
script? If yes, how?
回答1:
As for the mouse movement, you could check the position of the pointer and calculate if there is a change over time.
$p1 = [System.Windows.Forms.Cursor]::Position
Start-Sleep -Seconds 5 # or use a shorter intervall with the -milliseconds parameter
$p2 = [System.Windows.Forms.Cursor]::Position
if($p1.X -eq $p2.X -and $p1.Y -eq $p2.Y) {
"The mouse did not move"
} else {
"The mouse moved"
}
As for the keys, you might want to try a similar technique utilizing the get-keystroke script (which is basically a keylogger).
来源:https://stackoverflow.com/questions/35124097/check-if-mouse-keyboard-is-active-using-batch-or-powershell