AutoHotKey中如何将GUI获取的变量与数字相乘?消息刷屏脚本Sleep延迟失效问题求助
Hey there! Let's break down why your sleep interval isn't working right and get your script running smoothly.
The Root of the Problem
In AutoHotkey v1 (which it looks like you're using), command parameters default to traditional variable expansion mode—not expression mode. When you wrote:
Sleep, %Interval% * 1000
AHK does two things here:
- Replaces
%Interval%with your input (e.g.,1) - Treats the entire string
1 * 1000as the sleep duration
But the Sleep command only reads the numeric part at the start of the string. It ignores everything after the first non-numeric character, so it ends up sleeping just 1 millisecond instead of 1000. That's why your messages spam instantly!
The Fix
To make AHK evaluate the multiplication as an expression, you need to explicitly trigger expression mode for the Sleep parameter. There are two easy ways to do this in v1:
Option 1: Use % (percent sign + space)
This tells AHK to treat the rest of the line as an expression:
Sleep, % Interval * 1000
Option 2: Wrap the expression in %()
This is another common syntax for forcing expression evaluation:
Sleep, % (Interval * 1000)
Either of these will correctly calculate Interval * 1000 and sleep for the intended number of milliseconds.
Updated Full Script
Here's your script with the fix plus a few small quality-of-life tweaks for a beginner-friendly experience:
#SingleInstance force ^6:: Gui, Add, Text, W200 +Center, Message to spam Gui, Add, Edit, W200 vTextt, Hello from AHK! ; Default message for testing Gui, Add, Text, W200 +Center, Number of messages Gui, Add, Edit, W200 vLooop, 5 ; Default loop count Gui, Add, Text, W200 +Center, Interval between messages (seconds) Gui, Add, Edit, W200 vInterval, 1 ; Default 1-second interval Gui, Add, Button, W200 gStart, Start Auto Spamming Gui, Show, Caption "Auto Spammer" ; Add a window title Return Start: Gui, Submit Sleep, 5000 ; 5-second delay to switch to your chat window ; Optional: Add input validation to prevent bad entries if (!RegExMatch(Looop, "^[0-9]+$") || !RegExMatch(Interval, "^[0-9.]+$")) { MsgBox, Please enter valid numbers for loop count and interval! Gui, Show ; Reopen the GUI to let the user correct input Return } ; Convert inputs to proper numeric types Looop := Looop + 0 Interval := Interval + 0 Loop, %Looop% { Send, %Textt% Sleep, % Interval * 1000 ; Fixed sleep calculation Send, {Enter} } Gui, Destroy Return esc::exitapp
Extra Tips for New AHK Users
- Input Validation: The regex checks I added prevent crashes if someone enters non-numeric values (like letters) in the loop count or interval fields. It's a small touch but saves frustration!
- Default Values: Adding default text to the input boxes lets you test the script without typing everything every time.
- Variable Names: You used
vLooop(double 'o')—just make sure you don't mix up spelling elsewhere in the script (consistency is key!). - Testing: When testing, use a small loop count (like 5) so you don't flood the chat accidentally.
内容的提问来源于stack exchange,提问作者Chaotic

