如何用AutoHotkey实现自动点击、指定区域截图并循环至按S键终止?
Hey there! Let's break down your two AutoHotkey automation loop tasks with practical, easy-to-adjust scripts. I'll walk you through each one so you can tweak them to fit your exact needs.
Task 1: Auto Click "Next Page" + Full Screenshot (Manual Termination)
This script will repeatedly click a specified "Next Page" button, take a full-screen screenshot, and save it—until you manually close the script (via the tray icon or Ctrl+C in the console).
How to use:
- Use AutoHotkey's built-in Window Spy (right-click the tray icon > Window Spy) to get the coordinates of your "Next Page" button.
- Replace the
x100 y200values in the script with your button's coordinates. - Set your preferred screenshot save path (default is
C:\Screenshots\—make sure this folder exists first!).
#Persistent ; Keep script running after auto-execute section SetBatchLines, -1 ; Run at maximum speed ; Configure your settings here nextPageX := 100 ; Replace with your "Next Page" button X coordinate nextPageY := 200 ; Replace with your "Next Page" button Y coordinate screenshotPath := "C:\Screenshots\Page_" delayBetweenClicks := 2000 ; Wait 2 seconds between actions (adjust as needed) Loop { ; Click the "Next Page" button Click, %nextPageX%, %nextPageY% ; Wait for the page to load (adjust delay if needed) Sleep, %delayBetweenClicks% ; Take full-screen screenshot and save with sequential numbering FormatTime, timestamp,, yyyyMMdd_HHmmss FileAppend,, %screenshotPath%%timestamp%.png ; Create empty file to check path if ErrorLevel { MsgBox, Error: Could not save screenshot. Check if the folder exists! ExitApp } Send, {PrintScreen} ClipWait, 2 ; Wait for screenshot to be copied to clipboard if !ErrorLevel { FileDelete, %screenshotPath%%timestamp%.png Clipboard := ClipboardAll FileAppend, %Clipboard%, %screenshotPath%%timestamp%.png Clipboard := "" } } ; To stop the script: Right-click the AutoHotkey tray icon > Exit
Notes:
- Adjust
delayBetweenClicksbased on how long your page takes to load. If the page loads slow, increase this value (in milliseconds). - The screenshots are saved with a timestamp to avoid overwriting files.
Task 2: Auto Click Specific Position + Custom Region Screenshot (Stop with "S" Key)
This script will click a fixed screen position, capture a pre-defined region of the screen, save the image, and repeat—until you press the S key to stop it.
How to use:
- Use Window Spy to get:
- The coordinates of the position you want to click (
clickX,clickY) - The top-left corner (
regionX,regionY) and width/height (regionWidth,regionHeight) of the area you want to screenshot
- The coordinates of the position you want to click (
- Set your save path (again, ensure the folder exists).
#Persistent SetBatchLines, -1 ; Configure your settings here clickX := 300 ; X coordinate of the position to click clickY := 400 ; Y coordinate of the position to click regionX := 50 ; Top-left X of the screenshot region regionY := 50 ; Top-left Y of the screenshot region regionWidth := 800 ; Width of the screenshot region regionHeight := 600 ; Height of the screenshot region screenshotPath := "C:\CustomScreenshots\Region_" delayBetweenActions := 1500 ; Wait 1.5 seconds between click and screenshot ; Hotkey to stop the script: Press "S" s:: MsgBox, Script stopped successfully! ExitApp return Loop { ; Click the specified position Click, %clickX%, %clickY% ; Wait for the action to take effect Sleep, %delayBetweenActions% ; Capture the custom region and save FormatTime, timestamp,, yyyyMMdd_HHmmss saveFile := %screenshotPath%%timestamp%.png ; Use GDI+ to capture the region (more reliable than PrintScreen for custom areas) pToken := Gdip_Startup() pBitmap := Gdip_BitmapFromScreen(regionX "|" regionY "|" regionWidth "|" regionHeight) Gdip_SaveBitmapToFile(pBitmap, saveFile) Gdip_DisposeImage(pBitmap) Gdip_Shutdown(pToken) }
Important Notes:
- This script uses the GDI+ library (built into AutoHotkey v1.1+), so no extra downloads are needed.
- If you're using AutoHotkey v2, the syntax will be slightly different—let me know if you need a v2 version!
- The
Skey is set as the stop hotkey; you can change this to any key by replacings::with another key (e.g.,Esc::for Escape).
内容的提问来源于stack exchange,提问作者Flex
相关产品推荐
相关产品推荐

