To add clickable buttons that set predefined min and max values to your InputBox in AutoHotkey v2, I'll need to replace the standard InputBox with a custom GUI. This will allow for dynamic buttons loaded from a configuration file. Here's how to implement this feature:
First, let's update your script:
^+C::
{
global minNumber, maxNumber
ShowRangeSelector(minNumber, maxNumber)
}
; Function to display a custom GUI with configurable buttons
ShowRangeSelector(currentMin := "", currentMax := "")
{
; Create the main GUI
rangeGui := Gui(, "Range Configuration")
; Add input field with current values
rangeGui.Add("Text",, "Enter min and max values (format: min,max):")
inputField := rangeGui.Add("Edit", "w200", currentMin . "," . currentMax)
; Add default buttons
submitBtn := rangeGui.Add("Button", "Default w80", "Submit")
cancelBtn := rangeGui.Add("Button", "x+10 w80", "Cancel")
; Load custom button configurations from file
buttonConfigs := LoadButtonConfigs()
; Add a separator line
rangeGui.Add("Text", "w280 h1 0x10") ; Horizontal line
rangeGui.Add("Text",, "Quick Select:")
; Track button positions
btnX := 10
btnY := 110 ; Starting Y position for buttons
maxWidth := 280 ; Maximum width for the button area
; Add configurable buttons dynamically
for index, config in buttonConfigs
{
; Parse button config (format: "Display Text|min,max")
parts := StrSplit(config, "|")
buttonText := parts[1]
buttonValues := parts.Length > 1 ? parts[2] : buttonText
; Create the button
btn := rangeGui.Add("Button", "x" btnX " y" btnY " w80", buttonText)
; Set up event handler for this button
btn.OnEvent("Click", ButtonHandler.Bind(buttonValues, rangeGui, inputField))
; Update position for next button
btnX += 90
if (btnX > maxWidth)
{
btnX := 10
btnY += 30
}
}
; Set up event handlers
submitBtn.OnEvent("Click", SubmitHandler.Bind(rangeGui, inputField))
cancelBtn.OnEvent("Click", (*) => rangeGui.Destroy())
; Show the GUI and wait for user input
rangeGui.Show()
; Handle submit event
SubmitHandler(gui, input, *)
{
result := input.Value
gui.Destroy()
if (result)
{
splitNums := StrSplit(result, ",")
if (splitNums.Length >= 2)
{
global minNumber := splitNums[1]
global maxNumber := splitNums[2]
}
}
}
; Handle button clicks
ButtonHandler(values, gui, input, *)
{
input.Value := values
SubmitHandler(gui, input)
}
}
; Function to load button configurations from file
LoadButtonConfigs()
{
configs := []
configFile := A_ScriptDir . "\range_buttons.txt"
; Create default config file if it doesn't exist
if !FileExist(configFile)
{
defaultConfigs := "Small Range|1,10`nMedium Range|1,100`nLarge Range|1,1000`nCustom Text Button|5,50"
FileAppend(defaultConfigs, configFile, "UTF-8")
}
; Read the config file
try
{
fileContent := FileRead(configFile, "UTF-8")
lines := StrSplit(fileContent, "`n", "`r")
for index, line in lines
{
if (line != "")
configs.Push(line)
}
}
catch as err
{
MsgBox("Error reading config file: " . err.Message)
}
return configs
}
The implementation uses a configuration file called range_buttons.txt located in the same directory as your script. The format of this file is as follows:
Button Display Text|min,max
For example:
Small Range|1,10
Medium Range|1,100
Large Range|1,1000
Custom Text Button|5,50
How It Works
-
Custom GUI: Instead of a standard InputBox, this creates a custom window with an input field and dynamically loaded buttons.
-
Configuration File: The buttons are defined in an external text file (range_buttons.txt), making it easy to add or modify buttons without changing the script.
-
Dynamic Button Creation: The script reads the configuration file and creates buttons with custom labels and values.
-
Format Flexibility: Each line in the configuration file follows the format Display Text|min,max. If the pipe character is omitted, the display text is also used as the values.
-
Auto-Creation: If the configuration file doesn't exist, the script creates a default one with sample button configurations.
To Use
-
When you press Ctrl+Shift+C, the custom GUI appears with the current min/max values in the input field.
-
You can either:
- Manually enter values in the format "min,max" and click Submit
- Click any of the predefined buttons to instantly set those values and close the dialog
-
To modify the buttons, edit the range_buttons.txt file. Each line represents one button.
This implementation gives you a flexible and user-friendly interface for setting your min/max values, with the ability to easily configure and extend the available quick-select options[3][8].