2

I'm trying to keep my computer from going into idle mode. So I'm trying to write a script that will wiggle my mouse. This is a slightly customized version of a powershell script I found.

param($cycles = 60)


Add-Type -AssemblyName System.Windows.Forms
$screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
for ($i = 0; $i -lt $cycles; $i++) {

Start-Sleep -Seconds 3

[Windows.Forms.Cursor]::Position = "$($screen.Width),$($screen.Height)"
Start-Sleep -Seconds 3

[Windows.Forms.Cursor]::Position = "$($screen.Left),$($screen.Top)"
}

While this does wiggle the mouse it doesn't keep the screen from turning off. So I wrote this: (in python)

import ctypes, time, datetime

mouse_event = ctypes.windll.user32.mouse_event
MOUSEEVENTF_MOVE = 0x0001
print("press ctrl-c to end mouse shaker")
try:
    while True:
        mouse_event(MOUSEEVENTF_MOVE,25,0,0,0)
        time.sleep(1)
        mouse_event(MOUSEEVENTF_MOVE,0,25,0,0)
        time.sleep(1)
        mouse_event(MOUSEEVENTF_MOVE,-25,0,0,0)
        time.sleep(1)
        mouse_event(MOUSEEVENTF_MOVE,0,-25,0,0)
        time.sleep(1)
except KeyboardInterrupt:
    pass

This python code will keep my screen from going to sleep and prevents the machine from becoming idle. I believe the issue is because in the powershell scrip I never send the os the "MOUSEEVENTF_MOVE = 0x0001" string. According to the Microsoft website the variable MOUSEEVENTF_MOVE is a flag to signify that the mouse has moved.

Additionally I found this SO question (How i can send mouse click in powershell) that seems to be doing this exact thing but is also clicking, which I don't want. I've tried just commenting out those lines but the "SendImput" I think is expecting some input causing it to fail.

So here is my question. How do I pass the MOUSEEVENTF_MOVE variable in the powershell code? I'm thinking it should have the same effect as my python code.

P.S. This is my first time working with powershell.

Jarrod
  • 609
  • 2
  • 8
  • 16
  • 1
    There are alternative solutions: [using sendkeys](https://stackoverflow.com/questions/15835941/powershell-mouse-move-does-not-prevent-idle-mode) and [suspending current power plan](http://blog.backslasher.net/windows-awake-ps.html) – wOxxOm Aug 12 '17 at 06:15
  • Use [caffine](http://www.zhornsoftware.co.uk/caffeine/) from Zhorn Software – Dave Sexton Aug 12 '17 at 17:59
  • For my particular problem I actually need the mouse to move and the OS to know that it has moved. I really do like you suggestions though. I will keep those in mind for next time. :) – Jarrod Aug 12 '17 at 20:39
  • Python works great on Windows! Powershell not attempted with this approach. I did try the Powershell approach sending Scroll-Lock keystrokes and that failed. – Kevin Buchs Jun 02 '22 at 18:49

0 Answers0