Click命令内直接计算写法失效,如何修复?
Hey there! Let's break down why those initial Click attempts didn't work, and walk through simple fixes for your AutoHotkey script.
The Root Cause: AHK's Traditional vs Expression Syntax
AutoHotkey commands (like Click) use traditional syntax by default for their parameters. Here's what that means for your code:
- When you write
%width%, it just replaces the variable with its raw value (e.g., ifwidth=800, it becomes800). - Any extra text after that (like
- 30) is treated as plain, unprocessed text—not a math operation.
So your first attempt:
click, %width% - 30, %height% - 30
...actually sends the string 800 - 30 as the X-coordinate, not the calculated value 770. The Click command can't parse that messy string as a valid number, so it does nothing.
Your second try with parentheses:
click, (%width% - 30), (%height% - 30)
...still treats the whole thing as text ((800 - 30)), which is also not a valid coordinate. Parentheses don't trigger math in traditional syntax—they're just regular characters here.
Fixes You Can Use
Fix 1: Force Expression Mode for Click Parameters
You can tell AHK to treat the parameters as an expression by adding % (percent sign + space) before each calculation. This tells AHK to evaluate the math instead of treating it as text:
f1:: winGetActiveStats, title, width, height, topLeftX, topLeftY click, % width - 30, % height - 30 return
This will correctly compute width-30 and height-30 as numeric coordinates.
Fix 2: Pre-Calculate Variables (Your Working Approach)
Your existing solution is totally valid and often easier to read, especially if you need to reuse the coordinates later:
f1:: winGetActiveStats, title, width, height, topLeftX, topLeftY x := width - 30, y := height - 30 click, %x%, %y% return
The := operator uses expression mode by default, so the math is calculated correctly here.
Quick Tip for Future Reference
- Use
%var%for simple variable substitution in traditional command parameters. - Use
% expressionwhen you need to do math, function calls, or other calculations in command parameters. - Use
variable := expressionfor assignment statements (always uses expression mode).
内容的提问来源于stack exchange,提问作者john c. j.

