home / skills / sarfraznawaz2005 / agent-skills-collection / autohotkey-v2-gui

autohotkey-v2-gui skill

/autohotkey-v2-gui

This skill helps you build advanced AutoHotkey v2 GUI applications with event handling, data submission, and performance optimization across complex controls.

npx playbooks add skill sarfraznawaz2005/agent-skills-collection --skill autohotkey-v2-gui

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.7 KB
---
name: autohotkey-v2-gui
description: AutoHotkey v2 GUI development for advanced applications. Use when creating windows, handling events, optimizing performance, or working with controls like ListView, ComboBox, CheckBox. Covers event handling, data submission, positioning, and common GUI patterns.
---

# AutoHotkey v2 GUI Applications

Advanced GUI development patterns and optimization techniques for AutoHotkey v2.

## When to Apply

- Building complex GUI applications with multiple controls
- Implementing event-driven user interfaces  
- Optimizing GUI performance and memory usage
- Working with advanced controls like ListView, ComboBox, Tab

## Critical Rules

**Event Handling**: Use OnEvent method, not g-labels

```autohotkey
; WRONG - v1 g-label syntax
MyGui.Add("Button", "gButtonClick", "OK")

; RIGHT - v2 OnEvent method
Btn := MyGui.Add("Button", "w80", "OK")
Btn.OnEvent("Click", ButtonClick)
```

**Data Submission**: Use Submit() method with variable names

```autohotkey
; WRONG - accessing controls directly
EditText := MyGui["MyEdit"].Text

; RIGHT - Submit returns object with named values
MyGui.Add("Edit", "vUserName")
Data := MyGui.Submit()
UserName := Data.UserName
```

**Control Visibility**: Use Visible property, not Show/Hide commands

```autohotkey
; WRONG - v1 command syntax
GuiControl, Hide, MyButton

; RIGHT - v2 property syntax
MyButton.Visible := false
```

## Key Patterns

### Basic GUI Structure

```autohotkey
MyGui := Gui("+Resize", "Application Title")
MyGui.OnEvent("Close", AppClose)

; Add controls with variables for data retrieval
NameEdit := MyGui.Add("Edit", "vUserName w200")
SubmitBtn := MyGui.Add("Button", "Default w80", "OK")
SubmitBtn.OnEvent("Click", ProcessForm)

MyGui.Show("w300 h150")

ProcessForm(*) {
    Data := MyGui.Submit()
    MsgBox("Hello " . Data.UserName)
}

AppClose(*) {
    ExitApp
}
```

### ListView with Event Handling

```autohotkey
MyGui := Gui()
LV := MyGui.Add("ListView", "r20 w700", ["Name", "Size (KB)"])
LV.OnEvent("DoubleClick", LV_DoubleClick)

; Populate ListView
Loop Files, A_MyDocuments "\*.*"
    LV.Add(, A_LoopFileName, A_LoopFileSizeKB)

LV.ModifyCol()  ; Auto-size columns
LV.ModifyCol(2, "Integer")  ; Enable numeric sorting

LV_DoubleClick(LV, RowNumber) {
    RowText := LV.GetText(RowNumber)
    MsgBox("Selected: " . RowText)
}
```

### Tab Control with Organized Layout

```autohotkey
MyGui := Gui()
Tab := MyGui.Add("Tab3",, ["Settings", "Data", "Output"])

; First tab controls
MyGui.Add("CheckBox", "vAutoStart", "Start automatically")
MyGui.Add("ComboBox", "vTheme", ["Light", "Dark", "Auto"])

; Second tab controls  
Tab.UseTab(2)
MyGui.Add("Edit", "vDataPath w200")
MyGui.Add("Button", "x+10 yp", "Browse...")

; Third tab controls
Tab.UseTab(3)
MyGui.Add("Edit", "vOutput r10 w300")

Tab.UseTab()  ; End tab association
MyGui.Add("Button", "Default", "Apply")
```

### Modal Dialog Pattern

```autohotkey
MainGui := Gui("+Resize", "Main Window")
MainGui.Opt("+OwnDialogs")  ; Make dialogs modal

ShowSettingsBtn := MainGui.Add("Button", "w100", "Settings")
ShowSettingsBtn.OnEvent("Click", ShowSettings)

ShowSettings(*) {
    ; Modal dialogs prevent interaction with main window
    Result := MsgBox("Save current settings?", "Confirm", "YesNoCancel")
    if (Result = "Yes") {
        ; Save logic here
    }
}
```

## Performance Optimization

### Memory Management for Large Data

```autohotkey
; Pre-allocate string capacity for concatenation
VarSetStrCapacity(&LargeString, 5120000)  ; ~10 MB

Loop Files, "C:\*.*", "R"
    LargeString .= A_LoopFileFullPath . "`n"

; Free memory when done
LargeString := ""
```

### Object Capacity Tuning

```autohotkey
; Pre-allocate array/map capacity for performance
DataArray := []
DataArray.Capacity := 1000  ; Avoid frequent reallocation

DataMap := Map()
DataMap.Capacity := 500
```

## Advanced Control Patterns

### ComboBox with Validation

```autohotkey
ColorBox := MyGui.Add("ComboBox", "vColor", ["Red", "Green", "Blue"])
ColorBox.OnEvent("Change", ValidateColor)

ValidateColor(Ctrl, *) {
    if (Ctrl.Text = "Red")
        MsgBox("Warning: Red selected!")
}
```

### Dynamic Control Management

```autohotkey
; Get control position for dynamic layouts
MyEdit.GetPos(&x, &y, &w, &h)
NewBtn := MyGui.Add("Button", "x" . (x + w + 10) . " y" . y, "Next")

; Retrieve control values without Submit
CurrentText := ControlGetText("Edit1", MyGui.Hwnd)
```

## Common Mistakes

- **Missing variable names**: Controls need "v" prefix for Submit() to retrieve values
- **Incorrect positioning**: Use "xm ym" to reset to margins, "x+m y+m" for relative positioning
- **Event handler parameters**: Modern event handlers receive (Ctrl, Info) parameters, not just (*)
- **Window activation timing**: Use NoActivate option when showing background windows to avoid focus stealing

Overview

This skill provides patterns and practical techniques for building advanced AutoHotkey v2 GUIs. It focuses on modern v2 idioms: event-based handlers, Submit-based data retrieval, property-driven visibility, and control optimization. Use it to create responsive windows, complex layouts, and performant control-heavy applications.

How this skill works

The skill demonstrates how to construct GUIs by creating Gui objects, adding controls with named variables, and wiring events via OnEvent instead of legacy g-labels. It shows submitting form data through Gui.Submit(), managing control visibility with properties, using ListView/Tab/ComboBox patterns, and tuning memory or container capacity for large datasets. Examples include modal dialog patterns, dynamic control positioning, and efficient ListView population.

When to use it

  • Building multi-tab or multi-control desktop applications with AutoHotkey v2
  • Implementing event-driven interactions (Click, Change, DoubleClick) without g-labels
  • Handling large datasets in ListView or bulk UI updates while minimizing memory churn
  • Creating modal dialogs or background windows that should not steal focus
  • Validating ComboBox or Edit inputs and reacting to user changes in real time

Best practices

  • Use Ctrl.OnEvent('Event', Handler) for all events; avoid legacy g-label syntax
  • Assign v-prefixed variable names to controls and call Gui.Submit() to collect values
  • Toggle control visibility via the Visible property (Control.Visible := false/true)
  • Pre-allocate Array/Map capacity and use VarSetStrCapacity for large string builds
  • Keep event handlers small and retrieve values via Submit or explicit control access

Example use cases

  • A settings dialog with Tab control separating options, data paths, and output
  • A file browser that populates a ListView and handles DoubleClick to open items
  • A form-based tool that collects user inputs with Submit() and validates via OnEvent
  • An optimization script that loads thousands of entries into a ListView with pre-sized arrays
  • A modal preferences window that prevents interaction with the main application

FAQ

Why use Gui.Submit() instead of reading control.Text directly?

Submit collects named values reliably and is the recommended v2 pattern; direct Text access can bypass variable bindings and misses v-prefixed control variable mapping.

How do I prevent focus stealing when showing background windows?

Use the NoActivate option on Gui.Show or set Gui.Opt('+OwnDialogs') for modal behavior when appropriate.