Put PC to sleep if the mouse doesn't move Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahkgawd
Posts: 2
Joined: 31 Mar 2023, 19:49

Put PC to sleep if the mouse doesn't move

Post by ahkgawd » 31 Mar 2023, 20:00

So, I'm trying to put my PC to sleep if the mouse hasn't moved for one hour, basically what Windows can already do with built-in power saving settings but I just want to do it programmatically but I can barely understand the logic behind that so I found a similar script on the forum and tweaked it a little bit, but it just doesn't work as intended.

Code: Select all

#NoTrayIcon
while count<3600
    {
     MouseGetPos,x1,y1
     Sleep,1000
     count++
     MouseGetPos,x2,y2
     If ((x1<>x2) or (y1<>y2))             ;checking to see if the mouse has moved.
         {
         Sleep,1
         }
    }
DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0)		;put PC to sleep

[Mod action: Moved topic to v1 section. The main section is for v2.]

User avatar
mikeyww
Posts: 26605
Joined: 09 Sep 2014, 18:38

Re: Put PC to sleep if the mouse doesn't move  Topic is solved

Post by mikeyww » 31 Mar 2023, 20:55

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33
#Persistent
#InstallMouseHook
Process Priority,, B
SetTimer check, 30000

check() {
 Static threshold := 60000 * MINS := 60
 If (A_TimeIdleMouse > threshold)
  SendMessage, 0x112, 0xF170, 2,, Program Manager ; Sleep computer
}

ahkgawd
Posts: 2
Joined: 31 Mar 2023, 19:49

Re: Put PC to sleep if the mouse doesn't move

Post by ahkgawd » 01 Apr 2023, 07:30

Glad to be a part of the community, appreciate the help a lot, mikeyww!

Post Reply

Return to “Ask for Help (v1)”