Okay, modifying InputBox to include clickable buttons with custom actions and configurable values isn't directly possible with the standard InputBox command in AutoHotkey v2, as it's designed for simple text input.
To achieve the functionality you described – a window with manual input fields and clickable buttons that set variables and close the window, with button configurations from an external file – you'll need to create a custom GUI window using the Gui command.
Here's an AutoHotkey v2 script that implements this using a custom GUI and reads button configurations from a range_configs.txt file.
1. Create the Configuration File (range_configs.txt)
Create a plain text file named range_configs.txt in the same directory as your AHK script. Add lines in the format Button Text=min,max. Lines starting with a semicolon ; are treated as comments.
Example range_configs.txt:
; Define your predefined range buttons here
; Format: Button Text=min_value,max_value
Default Range=1,100
Small Range=5,10
Medium Range=20,50
Large Range=0,1000
2. The AutoHotkey v2 Script
Save the following code as an .ahk file.
#Requires AutoHotkey v2.0
; Global variables to store the min and max numbers
global minNumber := 1
global maxNumber := 100
; Global map to store button configurations loaded from the file
global RangeConfigMap := {}
; Function to load configuration from a text file
LoadRangeConfigs(configFile) {
local configMap := {}
if FileExist(configFile) {
Loop Read, configFile
{
local line := A_LoopReadLine
; Skip empty lines or lines starting with a semicolon (comments)
if (line = "" or SubStr(line, 1, 1) = ";")
continue
; Split the line into button text and range values
local parts := StrSplit(line, "=")
if (parts.Length >= 2) {
local buttonText := parts[1]
local rangeParts := StrSplit(parts[2], ",")
; Ensure we have two range values (min and max)
if (rangeParts.Length >= 2) {
local minVal := rangeParts[1]
local maxVal := rangeParts[2]
; Basic validation: check if minVal and maxVal are numbers (integers in this case)
if (minVal is integer and maxVal is integer) {
configMap[buttonText] := [minVal, maxVal]
} else {
; Optional: display a warning for malformed lines during development
; MsgBox("Warning: Skipping invalid range format in line: " . line)
}
} else {
; Optional: display a warning for malformed lines during development
; MsgBox("Warning: Skipping invalid range format in line: " . line)
}
} else {
; Optional: display a warning for malformed lines during development
; MsgBox("Warning: Skipping invalid format in line: " . line)
}
}
} else {
; Optional: display a message if the configuration file is not found
; MsgBox("Configuration file not found: " . configFile)
}
return configMap
}
; Hotkey to open the range configuration GUI
^+C:: ; Ctrl+Shift+C
{
; Load configurations from the file when the hotkey is pressed
; Use A_ScriptDir to make the path relative to the script's directory
RangeConfigMap := LoadRangeConfigs(A_ScriptDir . "\range_configs.txt")
; Create a new GUI window
myGui := Gui("+AlwaysOnTop") ; +AlwaysOnTop keeps the window above others
; Add controls for manual input
myGui.Add("Text", , "Enter min and max values (e.g., 1,100):")
; Add an Edit control to display and accept manual input
; 'vManualRange' associates this control's value with a variable named ManualRange in the Gui object
myGui.Add("Edit", "w 180 vManualRange", minNumber . "," . maxNumber)
; Add OK and Cancel buttons for the manual input
; 'gGuiOK' and 'gGuiCancel' specify the label/subroutine to run when the button is clicked
myGui.Add("Button", "x+m w 70 gGuiOK Default", "OK") ; 'Default' makes this the button activated by Enter
myGui.Add("Button", "x+m w 70 gGuiCancel", "Cancel")
; Add a separator text before the custom buttons
; 'xm' starts the control in the leftmost column, 'y+15' places it 15 pixels below the previous control
myGui.Add("Text", "xm y+15", "Or select a predefined range:")
; Position variable for the custom buttons, starting below the separator text
local buttonY := "y+5"
; Dynamically add buttons based on the loaded configuration
if (RangeConfigMap.Count = 0) {
; Display a message if no predefined ranges were found
myGui.Add("Text", "xm " . buttonY, "(No predefined ranges found)")
} else {
; Loop through the map and create a button for each configuration
for buttonText, rangeArray in RangeConfigMap {
; Add a Button control
; 'xm' starts the button in the leftmost column aligned with controls above
; buttonY positions the button vertically, starting with y+5 and then y+m for subsequent buttons
; 'w 150' sets the width of the button
; 'gSetRange' is the common label/subroutine for all custom range buttons
myGui.Add("Button", "xm " . buttonY . " w 150 gSetRange", buttonText)
buttonY := "y+m" ; Position the next button below the current one with a standard margin
}
}
; Automatically size the window to fit the controls and show it
myGui.Show()
; The script execution pauses here until one of the GUI labels (GuiOK, GuiCancel, SetRange, GuiClose) is triggered.
}
; --- GUI Event Handlers ---
; Handler for the OK button (manual input)
GuiOK:
{
; Get the current values from the Edit control without destroying the GUI
myGui.Submit(False)
local manualInput := myGui.ManualRange
local splitNums := StrSplit(manualInput, ",")
if (splitNums.Length >= 2) {
local minVal := splitNums[1]
local maxVal := splitNums[2]
; Validate if the input values are integers
if (minVal is integer and maxVal is integer) {
; Update the global variables
global minNumber := minVal
global maxNumber := maxVal
myGui.Destroy() ; Close the GUI
} else {
MsgBox("Invalid input format. Please enter two numbers separated by a comma (e.g., 1,100).", "Error", "IconW")
; Keep the GUI open for the user to correct the input
}
} else {
MsgBox("Invalid input format. Please enter two numbers separated by a comma (e.g., 1,100).", "Error", "IconW")
; Keep the GUI open
}
return ; Important for gLabels
}
; Handler for the Cancel button
GuiCancel:
{
myGui.Destroy() ; Close the GUI without changing variables
return
}
; Handler for any of the custom range buttons
SetRange:
{
; A_GuiControl contains the text of the button that was clicked
local clickedButtonText := A_GuiControl
; Look up the corresponding min/max values in the global configuration map
if (RangeConfigMap.Has(clickedButtonText)) {
local rangeArray := RangeConfigMap[clickedButtonText]
; Update the global variables
global minNumber := rangeArray[1]
global maxNumber := rangeArray[2]
myGui.Destroy() ; Close the GUI
}
; If the button text somehow doesn't match an entry in the map,
; no action is taken (or you could add an error message).
return
}
; Handler for closing the GUI window (e.g., clicking the X button)
GuiClose:
{
myGui.Destroy() ; Close the GUI
return
}
; Keep the script running in the background to listen for the hotkey
#Persistent
Explanation:
#Requires AutoHotkey v2.0: Ensures the script is run with AHK v2.
- Global Variables:
minNumber and maxNumber store the current values. RangeConfigMap is a map (object) that will hold the button text and their corresponding [min, max] arrays loaded from the file.
LoadRangeConfigs(configFile) Function:
- Takes the configuration file path as input.
- Checks if the file exists.
- Reads the file line by line using
Loop Read.
- Ignores empty lines and lines starting with
;.
- Splits each valid line by
=. The first part is the button text, the second is the range string.
- Splits the range string by
, to get min and max values.
- Performs basic validation (
is integer) to ensure the values are numbers.
- Stores the
buttonText as a key and an array [minVal, maxVal] as the value in configMap.
- Returns the
configMap.
^+C:: Hotkey:
- This is the subroutine that runs when Ctrl+Shift+C is pressed.
- It calls
LoadRangeConfigs to populate RangeConfigMap. A_ScriptDir . "\range_configs.txt" constructs the full path to the config file in the same directory as the script.
- It creates a new
Gui object. +AlwaysOnTop keeps the window visible.
myGui.Add(...) adds controls to the window:
- A Text label.
- An Edit field (
vManualRange) pre-filled with the current minNumber,maxNumber. vManualRange allows accessing the content via myGui.ManualRange.
- OK and Cancel buttons with
gLabels (gGuiOK, gGuiCancel) that specify which subroutine to execute when clicked. Default makes the OK button active when Enter is pressed.
- A separator Text label.
- It then iterates through the
RangeConfigMap. For each entry, it adds a Button control.
- All custom range buttons use the same
gLabel: gSetRange.
- Positioning options (, , , ) are used to arrange controls vertically.
This approach provides the flexibility you need, separating the button configuration from the script logic and allowing easy addition or modification of predefined ranges.