Using AutoHotKey to keep Windows Awake

Let's say you have a laptop that you need to keep awake while a process is running. Let's also say you can't change the power settings due to group policy. Here's an AutoHotKey script that will simulate moving the mouse.

Setup AutoHotKey

AutoHotKey (ahk for short) is an application that will allow you to script all kinds of windows behavior.

You'll need a "portable" version of ahk for this project. The direct download link is below

Unzip this folder, inside you'll find a few executables and a few sample scripts. The file we want is AutoHotkeyU64.exe. Put this into a working directory, or on a flash drive.

The script

You'll need a script to work with the ahk executable. I've enclosed the one I developed for this project below. This script will "move" the mouse a tiny amount every second. The amount of movement is determined by a random function.

    #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
    #Warn  ; Enable warnings to assist with detecting common errors.
    #Persistent ; Make this thing stay open!
    SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

    Rand( a=0.0, b=1 ) {
       IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
       Else Random,r,a,b
       Return r

    SetTimer, MouseMove, 1000 ; do this every 30 seconds
      MouseMove,Rand(),Rand(),0,R ; mouse pointer stays in place but sends a mouse event

Copy this script to a new file, give it a meaningful name and the .ahk extension. Copy the file to your working directory or flash drive.

Run the script

With the script ready to go, open your working directory or flash drive in either PowerShell or windows command prompt and run the following

AutoHotkeyU64.exe script.ahk

If successful, a little "H" icon will pop up in the notification area and your machine will now stay awake

Happy Hacking!

This page was last updated on June 18, 2020
Go back to the home page