Issues with AutoHotKey symbol pasting from GUI - autohotkey

I am new to AHK, and am trying to make a script that opens a GUI with a short list of commonly-used symbols that can be chosen, and then be automatically be pasted.
My code so far:
!+q::
Gui, Add, ListBox, w100 h100, vSymbolChoice, ™|©|°|π|☭|☢|⚠|ツ|•|Ω
Gui, Add, Button, Default, Submit
Gui, Add, Button, default, Cancel
Gui, Show
return
ButtonSubmit:
Gui, Submit
Sleep, 1000
Send, %SymbolChoice%
Gui, Destroy
ButtonCancel:
Gui, Destroy
It creates the GUI and the ListBox but doesn't paste the symbol when I have it selcted and press submit.
Also, is there a better way to detect if a text field is selected than just waiting a second and hoping the user selected the field in that time?

; auto-execute section
; create and show the Gui
Gui, Add, ListBox, w100 h130 vSymbolChoice, ™|©|°|π|☭|☢|⚠|ツ|•|Ω
Gui, Add, Button, Default, Submit
Gui, Add, Button,, Hide ; you can't have two default buttons on a Gui
Gui, Show
return
; Press Alt+Shift+Q to show the hidden Gui after ButtonSubmit or ButtonHide
!+q:: Gui, Show
ButtonSubmit:
GuiControlGet, SymbolChoice ; get the control's contents stored in the variable SymbolChoice (retrieves the ListBox's current selection)
Gui, Submit ; saves the contents of this control to its associated variable SymbolChoice
SendInput, %SymbolChoice%
return
; Hide the Gui
ButtonHide:
Gui, hide
return
; Press ESC or close the Gui to terminate the script
GuiClose:
Esc:: ExitApp
For details see GUI in the documentation.

Related

Trying to change variable using a checkbox in AHK, but it doesn't seem to be changing the variable at all?

I've been trying to make a pretty basic toggle button to turn hotkeys on and off, but it doesn't seem to be updating the "MyHotkeyCheckboxState" variable at all?
I tried changing it to this script to troubleshoot, but the box is just empty. Any ideas or anything big I'm missing?
; Create a GUI window with a checkbox control
Gui, Add, Checkbox, vMyHotkeyCheckboxState, Enable Hotkey
Gui, Show
; Create a hotkey using PgUp as the keybind
Hotkey, PgUp, MyHotkey, On
return
; Toggle the hotkey based on the state of the checkbox
MyHotkey:
MsgBox, %MyHotkeyCheckboxState%
return
The checkbox status wasn't saved to the variable yet when you checked for it in the MsgBox command.
You just need to add Gui, Submit, NoHide every time you want to check for the checkbox state. NoHide is there so that the target GUI doesn't get hidden.
; Create a GUI window with a checkbox control
Gui, Add, Checkbox, vMyHotkeyCheckboxState, Enable Hotkey
Gui, Show
; Create a hotkey using PgUp as the keybind
Hotkey, PgUp, MyHotkey, On
return
; Toggle the hotkey based on the state of the checkbox
MyHotkey:
Gui, Submit, NoHide
MsgBox, %MyHotkeyCheckboxState%
return
Or using GuiControlGet
; Create a GUI window with a checkbox control
Gui, Add, Checkbox, vMyHotkeyCheckboxState, Enable Hotkey
Gui, Show
; Create a hotkey using PgUp as the keybind
Hotkey, PgUp, MyHotkey
Return
; Toggle the hotkey based on the state of the checkbox
MyHotkey:
GuiControlGet, MyHotkeyCheckboxState
MsgBox, %MyHotkeyCheckboxState%
Return

AutoHotKey: Hotkey input does not display media keys correctly

I am trying to query a hotkey from a user using the hotkey gui control. It displays most of the entered hotkeys correctly. When I press one of the media keys (play/pause, previous track, next track, volume up/down/mute), though, the input just displays a single character. E.g. when I press "volume up":
When I output the entered key afterwards however, it is correctly displayed:
Minimal example:
Gui, Add, Hotkey, x10 y10 w150 h20 vMyHotkey, %MyHotkey%
Gui, Add, Button, x10 y40 w150 h20 gOK, OK
Gui, Show
return
OK:
Gui, Submit
Msgbox %MyHotkey%
return
Why is the hotkey not displayed correctly in the hotkey input gui control?

Use Enter button for submit, while working normally elsewhere

I have a script that opens a GUI with a simple interface, which closes with Esc, and submits through the button. The script always runs in the background. I'm trying to find a way to make the Enter key work as a submit, but while still working normally everywhere else. I've tried using IfWinActive, but it doesn't work. Any ideas? thanks! (btw some of the code looks messy in this format, but the "unnecessary details" are there because this isn't the full script.)
Lwin & MButton::
Goto, Start
return
Start:
IfWinNotExist, New
{
Gui,1:-border
gui, font, s24, Product Sans ; Set 10-point Product Sans.
Gui, Add, Text,, Type here to search: ;controls the UI
Gui, Add, Edit, vTextInput
Gui, Add, Button, -WantReturn gSubmit w80, Go
Gui, Show, w400 h300, New
IfWinExist, New
Hotkey, Enter, Submit, On
return
}
Else
Return
IfWinExist, New
Esc::Gui, Destroy
return
IfWinExist, New
Enter::
Goto, Submit
return
Submit:
Gui, Submit ;destroys the UI when submitted
GuiControlGet, TextInput
Gui, Destroy
Else
{
link = https://www.google.com/search?q= %TextInput% ;search google
Run, %link%
}
The code really looks like some real horror haha.
So much legacy AHK or stuff that doesn't work. But anyway, can't fix all that since that isn't the full script.
The answer to the actual question is to make your button the default button:
Gui, -Border
Gui, Font, s24, % "Product Sans"
Gui, Add, Text, , % "Type here to search:"
Gui, Add, Edit, vTextInput
Gui, Add, Button, +Default gSubmit w80, % "Go"
Gui, Show, w400 h300, % "New"
return
Submit:
Gui, Submit
Run, % "https://www.google.com/search?q=" TextInput
return
Sure, you could also define your own hotkey, like you tried to, but there's just no reason to when the above is so convenient.

How can I read multiple lines of user input in AutoHotkey?

I have an AutoHotkey script which needs to read multiple lines of employee data from a user.
InputBox, userInput, Employee Records, Please enter employee records. (One per line)
Unfortunately, an InputBox only allows users to enter a single line of text. Trying to add newlines with Enter will instead submit whatever data has been entered.
How can I take in multiple lines of user input in an AutoHotkey script?
This implements a generic multiline input function
F3::MsgBox % MultiLineInput( "Employee Records", "Please enter employee records (One per line):" )
MultiLineInput(title, prompt)
{
static input
input := ""
Gui, Add, Text,, %prompt%
Gui, Add, Edit, w400 h60 vinput
Gui, Add, Button, gokay_pressed, Okay
Gui, Add, Button, cancel X+8 YP+0, Cancel
Gui, Show, Center autosize, %title%
WinWaitClose %title%
return input
okay_pressed:
Gui Submit
Gui Destroy
return
GuiClose:
GuiEscape:
ButtonCancel:
Gui, Destroy
return
}
This demonstrates a multi-line input box
F2::
Gui, Add, Text,, Please enter employee records (One per line):
Gui, Add, Edit, w600 h60 vinput
Gui, Add, Button, gokay_pressed, Okay
Gui, Add, Button, cancel X+8 YP+0, Cancel
Gui, Show, Center autosize, Employee Records
Return
okay_pressed:
Gui Submit
Gui Destroy
MsgBox %input%
Return
GuiClose:
GuiEscape:
ButtonCancel:
Gui, Destroy
return

AutoHotkey ControlFocus fails for Edit Control

I have a window, created by AutoHotkey, with one button and one edit control on it.
What I am trying to do is to bring back the input focus to the Edit control after I click the button. By means of WinSpy (many thanks to Robert Kuster), I have the following information about the Edit control.
Handle : 0x00330786 (changes every time I start the application)
Control ID : 4
Class : Edit
Window Title : some window title
Parent Window Class : AutoHotkeyGUI
Below is the statement I am using in my script.
ControlFocus, MyEdit, some window title
BTW, AutoHotkey help suggests to leave the control name and replace the window title with HWND of the target control as an alternative. Please guide on how to accomplish this.
Below is a piece of Autohotkey script using
GuiControl, Focus, ControlName
in order to change the input focus to the Edit control (namely Completion) after button "Show My Progress" is clicked.
%WinTitle% = some window title
Gui, Add, Text, vLabel cWhite, Reading Completion
Gui, Add, Edit, vCompletion ym w40 ; The ym option starts a new column of controls.
Gui, Add, Progress, vMyProgress w300 h30 xp+50 yp-5
Gui, Add, Button, xp yp+50, Show My Progress
GuiControl,, Completion, %iniCompletion%
Gui, Show, h140, %WinTitle%
ControlClick, Show My Progress, %WinTitle%
**GuiControl, Focus, Completion**
return
ButtonShowMyProgress:
Gui, Submit, NoHide ; Save the input from the user to each control's associated variable.
MyCompletionPercentage:=(Completion / (PageEnd-PageStart+1)) * 100
PercentageRounded:=Round(MyCompletionPercentage,0)
GuiControl,, MyProgress, %PercentageRounded%
**GuiControl, Focus, Completion**
return
Many thanks to MCL and BGM for their comments ...
If I understand correctly, you have your own GUI built with AHK. Why
don't you use GuiControl, Focus? – MCL
MCL has a point. The GuiControl is autohotkey's way of working with it's own controls. You would only use ControlFocus if you are working with controls for
external windows. MCL - maybe you should present that as an answer. – BGM