AutoHotkey Script to Detect Screen Changes and Alert You

Published by Nyau Wai Hoe - Updated on

So, I made this super handy script with a friend’s help. It’s an AHK (AutoHotkey) script that makes a noise when something changes on your screen. This is perfect for when you’re working or playing games and need to keep an eye on something without staring at it the whole time. It’s a piece of cake to use and tweak for whatever you need. Let’s look into what this script does, how to get it set up, and some cool ways you might use it.

Also see: Starting an AutoHotkey Script on Windows 11/10 Startup

AutoHotkey Script to Detect Screen Changes and Alert You

A simple AHK script to monitor and detect screen changes with sound alert

Just copy this script, save it as something.ahk, and you’re good to go. Or keep reading to learn how to use it. Here’s the script:

#NoEnv
#Warn
#SingleInstance, Force
SetBatchLines, -1

SoundFilePath := "C:\alert.mp3"

; Default values
global defaultX := 123, defaultY := 342, defaultx2 := 243, defaulty2 := 548

; Create GUI
Gui, Add, Text, , X1:
Gui, Add, Edit, vX1 w50
Gui, Add, Text, , Y1:
Gui, Add, Edit, vY1 w50
Gui, Add, Text, , X2:
Gui, Add, Edit, vX2 w50
Gui, Add, Text, , Y2:
Gui, Add, Edit, vY2 w50
Gui, Add, Button, Default gSetDefault, Set Default
Gui, Add, Button, gStartMonitoring, Start Monitoring
Gui, Show,, Screen Region Monitor
return

SetDefault:
    GuiControl,, X1, %defaultX%
    GuiControl,, Y1, %defaultY%
    GuiControl,, X2, %defaultx2%
    GuiControl,, Y2, %defaulty2%
return

StartMonitoring:
    Gui, Submit  ; Get values from GUI
    global x1 := X1, y1 := Y1, x2 := X2, y2 := Y2
    global w := x2 - x1, h := y2 - y1
    Gui, Destroy  ; Close the GUI
    Loop {
        If (ScreenCompare(x1, y1, w, h, 0x0, [5000, 200]*)) {
            SoundPlay, %SoundFilePath%
            MsgBox, 4, , Screen Region Change Detected!`nContinue monitoring?
            IfMsgBox, No
                break
        }
    }
return

GuiClose:
ExitApp

ScreenCompare( X, Y, W, H, Hwnd:=0x0, Sleep* )  {
Local
Global A_Args
  Sleep[1] := Sleep[1]="" ? 100 : Format("{:d}", Sleep[1])
  Sleep[2] := Sleep[2]="" ? 100 : Format("{:d}", Sleep[2])

  VarSetCapacity(BITMAPINFO, 40, 0)
  NumPut(32, NumPut(1, NumPut(0-H*2, NumPut(W, NumPut(40,BITMAPINFO,"Int"),"Int"),"Int"),"Short"),"Short")

  hBM := DllCall("Gdi32.dll\CreateDIBSection", "Ptr",0, "Ptr",&BITMAPINFO, "Int",0, "PtrP",pBits := 0, "Ptr",0, "Int",0, "Ptr")
  sDC := DllCall("User32.dll\GetDC", "Ptr",(Hwnd := WinExist("ahk_id" . Hwnd)), "Ptr")
  mDC := DllCall("Gdi32.dll\CreateCompatibleDC", "Ptr",sDC, "Ptr")
  DllCall("Gdi32.dll\SaveDC", "Ptr",mDC)
  DllCall("Gdi32.dll\SelectObject", "Ptr",mDC, "Ptr",hBM)
  DllCall("Gdi32.dll\BitBlt", "Ptr",mDC, "Int",0, "Int",H, "Int",W, "Int",H, "Ptr",sDC, "Int",X, "Int",Y, "Int",0x40CC0020)

  A_Args.ScreenCompare := {"Wait": 1},   Bytes := W*H*4,  Count := 0
  hMod := DllCall("Kernel32.dll\LoadLibrary", "Str","ntdll.dll", "Ptr")
  While ( A_Args.ScreenCompare.Wait && (Count<2) )
  {
      DllCall("Gdi32.dll\BitBlt", "Ptr",mDC, "Int",0, "Int",0, "Int",W, "Int",H, "Ptr",sDC, "Int",X, "Int",Y, "Int",0x40CC0020)
      Count := ( (Byte := DllCall("ntdll.dll\RtlCompareMemory", "Ptr",pBits, "Ptr",pBits+Bytes, "Ptr",Bytes) ) != Bytes )
               ? (Count + 1) : 0
      Sleep % (Count ? Sleep[2] : Sleep[1])
  }   Byte +=1
  DllCall("Kernel32.dll\FreeLibrary", "Ptr",hMod)

  SX := (CX := Mod((Byte-1)//4, W) + X),    SY := (CY := (Byte-1) // (W*4)   + Y)
  If (Hwnd)
    VarsetCapacity(POINT,8,0), NumPut(CX,POINT,"Int"), NumPut(CY,POINT,"Int")
  , DllCall("User32.dll\ClientToScreen", "Ptr",Hwnd, "Ptr",&POINT)
  , SX := NumGet(POINT,0,"Int"),  SY := NumGet(POINT,4,"Int")

  If (Wait := A_Args.ScreenCompare.Wait)
      A_Args.ScreenCompare := { "Wait":0, "CX":CX, "CY":CY, "SX":SX, "SY":SY }
  DllCall("Gdi32.dll\RestoreDC", "Ptr",mDC, "Int",-1)
  DllCall("Gdi32.dll\DeleteDC", "Ptr",mDC)
  DllCall("User32.dll\ReleaseDC", "Ptr",Hwnd, "Ptr",sDC)
  DllCall("Gdi32.dll\DeleteObject", "Ptr",hBM)
Return ( !!Wait )
}

Simple AHK script to monitor and detect screen changes

How the script works to monitor your screen

This script is a handy tool that uses AutoHotkey, a kind of programming language for Windows. This script will watch a certain part of your screen for any changes. If it notices something different, it plays a sound to let you know. Here’s a simple breakdown of how it does its job:

  1. It sets itself up and makes sure only one of it is running at any time.
  2. At the start, you tell it which sound file to play when it sees a change.
  3. It has a simple setup screen where you can put in the coordinates on the screen you want watched.
  4. You pick a rectangular area by typing in the coordinates of two points. That’s the area it keeps an eye on.
  5. After you start it, it keeps taking pictures of that area and checks if anything changes. If yes, you hear your sound.
  6. After the sound plays, a pop-up asks if you want to keep going. This way, you can pause or stop it as needed.

This way, it doesn’t use up a lot of your computer’s power since it only looks at the area you chose.

Related resource: How to Auto Set Priority and Affinity in Windows 11/10 with Script

How to run the AHK script (for beginners)

Getting this AutoHotkey script going is pretty easy. Just follow these steps to get it up and running:

  1. First up, if you haven’t yet, go download and install AutoHotkey from its official website.
  2. Open “AutoHotkey Dash” and hit “New script”. You can name it whatever you like, say, “ScreenChangeDetector.ahk”.Create new AHK script
  3. Right-click your new script file and open it with any text editor. Copy the script we gave you and paste it into this file.Open AHK script in notepad
  4. In the script, find the line that starts with SoundFilePath :=. Change the example path to where your alert sound file is.Change the notification sound for screen changes detection script
  5. To start the script, just double-click it. Use the setup screen to type in the coordinates of 2 points where on the screen you want to watch. If you’re not sure about the exact coordinates, don’t worry, we’ll cover how to figure that out in a bit.Input coordinates of screen region for the script to detect changes
  6. Hit the “Start Monitoring” button. Now it’s keeping an eye on the spot you picked.
  7. When it sees a change, it’ll show a message and play your sound.Detect screen changes and notify with sound alert
  8. The script comes with some preset spots to watch, but you can change these to fit what you usually need, making it quicker to set up next time.default coordinates for screen changes detection script

And remember, you can stop the script anytime by clicking the AutoHotkey script icon in the taskbar and choosing “Exit”.

Practical uses of the script

This AutoHotkey script isn’t just for fun; it’s super useful for work or gaming. I made it for gaming, but here are some cool ways to use it:

Work automation

It can watch your screen for signs that a task is done, like a progress bar finishing up in video editing, or a data analysis wrapping up. The sound lets you know to move on without having to stare at your screen the whole time.

Gaming

For games, it can let you know when something happens in a specific part of the game, like when resources are ready or if you get a game invite.

Automatically detect screen changes in game

Stock trading (not recommended)

Watch specific stocks or financial signs on your trading screen. It’ll buzz you if there’s a sudden move.

Social media and web monitoring

Keep an eye on web pages for new stuff without hitting refresh all the time. Great for tickets, auctions, or waiting for updates.

How to find the coordinates, width and height with “Windows Spy”

To find out the right spot on your screen for the script to watch, you can use Windows Spy, a tool that comes with AutoHotkey. It’s easy to use for finding the area’s size and position. Here’s how:

  1. Start AutoHotkey and open “Window Spy”.AutoHotkey Window Spy Tool
  2. Make sure the part of the screen you want watched is active. Then, hover your mouse over the starting corner. Windows Spy will tell you the X and Y spots of your mouse.How to find the coordinates, width, and height of a region in Windows 11
  3. Write down those X and Y spots. They’re your starting point.
  4. Move your mouse to the opposite corner without clicking. The difference in X and Y spots from before gives you the size of the area.
  5. Type those coordinates into the script’s setup screen to get it work.

Final notes

I made this script to help me keep track of things in my games without having to glue my eyes to the screen. It’s awesome because it beeps at me when something changes. But, just a heads up, this script is super sensitive. It notices even tiny changes, like a single pixel moving. So, if the part of the screen you’re watching changes a lot on its own, this script might keep beeping at you over stuff you’re not looking for.


Nyau Wai Hoe
Nyau Wai Hoe is the Founder and Chief Editor of WindowsDigitals.com. With a degree in software engineering and over 12 years of experience in the tech support industry, Nyau has established himself as an expert in the field, with a primary focus on the Microsoft Windows operating system. As a tech enthusiast, he loves exploring new technologies and leveraging them to solve real-life problems.

Share via
Copy link