AutoHotkey Lesson 5: Variables, Input Boxes and Message Boxes

AutoHotkey Lesson 5: Variables, Input Boxes and Message Boxes featured illustration

πŸ“Ž This article includes 1 downloadable practice file ↓

⏱ 1 min read

πŸ“˜ AutoHotkey Beginner Course Β· Lesson 5 of 6

In this article
  1. Variables and expressions
  2. Ask for input
  3. Yes/No questions
  4. The clipboard
  5. Where people go wrong
  6. Practice
  7. Continue learning

Scripts become more useful when they can ask a question and react. AHK v2 has proper variables and expressions, plus simple dialog boxes.

Advertisement

Variables and expressions

amt := 2500
gst := Round(amt * 0.18, 2)
MsgBox "GST is " gst      ; text and variables joined by a space

Ask for input

ib := InputBox("Amount (Rs):", "GST calculator")
if ib.Result = "OK" && IsNumber(ib.Value)
    MsgBox "Total: " Number(ib.Value) * 1.18

Yes/No questions

if MsgBox("Copy the total?", "GST", "YesNo") = "Yes" runs code only when the user agrees.

The clipboard

A_Clipboard holds copied text: read it, change it (StrUpper, StrReplace, Trim) and paste it back. Great for cleaning copied text before pasting into forms.

Advertisement
πŸ’‘ Always validate input with IsNumber or a check before calculating; users will type “2,500” or leave the box empty.

Where people go wrong

Problem Cause
“2500” + 1 gives odd results Convert with Number()
Using = to assign v2 uses := to assign

Practice

Run the script and press Ctrl+Alt+G; then copy some text and press Ctrl+Alt+U in Notepad.

πŸ“Ž Practice files for this article

  • ⌨️
    Lesson 5 script (AutoHotkey v2)Double-click to run after installing AutoHotkey v2.
    ⬇ AHK Β· 549 B

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong