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

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

Related

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?

Close GUI but continue script in AHK

I'm trying to have a gui close but leave the script running. This is because I want to do other actions if the user chooses to escape the GUI by either hitting esc or simply clicking the 'X' in the upper right. I don't understand how I would leave the script running but close the gui. GUI close doesn't seem to do anything when clicking esc or the X. I've scanned through the GUI docs and cannot figure it out. They always run exitapp, but I'm not ready to exitapp, I need to do other things.
Gui, Add, Text, ,To cancel, press ESCAPE or close this window.
Gui, Show, w320 h80, Downloads
GuiClose:
GuiEscape:
; Continue on to do other things here!!!!
WinActivate ahk_exe notepad++.exe
; do things...
exitapp
What you're describing sounds like what I wanted to do, too. It was a lot of slogging and piecemealing but I think what you are looking for is Gui, Cancel Take a look at the code below and see if that doesn't solve your problem.
It doesn't use ExitApp and it doesn't destroy the window if you want to pop it up later. You'll have to figure how to place the rest of your code, but I believe this is what you are asking.
;Set the GUI window
Gui, Add, Text,, Hit Escape to Clear window when it is active
#F9:: ;show the window with WindowsKey-F9
Gui, Show,
return
;set the escape key to clear GUI window
GuiEscape: ; Note: single colon here, not double
Gui, Cancel
Return
It's a little trickier if you have multiple GUI windows. You must name each:
;Set the two GUI windows. In example, First is labeled as FirstGUI and second as SecondGUI. Notice how you separate with single colon.
Gui, FirstGUI:Add, Text,, Hit Escape to Clear FirstGUI window when it is active
Gui, SecondGUI:Add, Text,, Hit Escape to Clear SecondGUI window when it is active
#F9:: ;show the two windows ("xCenter y700" is used to prevent the windows from stacking.)
Gui, FirstGUI:Show, xCenter y700, Window Title of First GUI
Gui, SecondGUI:Show, xCenter y900, Window Title of Second GUI
return
;set the escape key to clear for each Gui independently.
FirstGUIGuiEscape: ; Note that you must add the Gui Name (FirstGUI) to the beginning of the "GuiEscape:" command and also name it in the following:
Gui, FirstGUI:Cancel
Return
SecondGUIGuiEscape: ; And here you must add SecondGUI as noted above.
Gui, SecondGUI:Cancel
Return
Answering a little late but hope this works for you or someone else!
They assume in the documentation that by clicking x, you'd want the script to close.
So they show ExitApp as an example.
If you don't want to do that though, of course no need to do it.
I think what you're after is destroying the gui:
GuiClose:
GuiEscape:
Gui, Destroy
return

Issues with AutoHotKey symbol pasting from GUI

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.

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

AHK GUI does not work

I'm using the following AHK script to pop up screen with checkboxes. Idea is that when I check on the boxes it should open a folder. It works for the first one, but when I check the second one nothing happens. Any ideas whats wrong with my code
Gui, Add, Checkbox, vOpen, Open Bedrijfsbureau
Gui, Add, Checkbox, vOpen2, Open
Gui, Add, Button, Default gButtonGo, Go
Gui, Show, w500 h300, Products
Return
ButtonGo:
Gui, Submit
Gui, Destroy
If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return
If Open2 = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau\Dimensioneren"
Return
GUIClose:
Gui, Destroy
When you use an if statement without curly brackets, only the following line is read by the if statement.
That means that when you do this:
If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return
...the program ends. The if statement does the run command and the return gets processed no matter what.
So, try this, instead, if you want multiple lines:
If(Open = 1){
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return
}
Also, nothing will happen anyway! First, when you check the box, you have no sub to handle the checkbox. In other words, when you click the checkbox, nothing is supposed to happen. So, I suppose that you only want things to happen when you push the "Go" button.
That's great, but your go button does two things: it grabs the value of the checkbox, then it exits the GUI ----> THIS IS WHY YOUR PROGRAM DOESN'T WORK.
You want to do this, instead:
Gui, Add, Checkbox, vOpen, Open Bedrijfsbureau
Gui, Add, Checkbox, vOpen2, Open
Gui, Add, Button, Default gButtonGo, Go
Gui, Show, w500 h300, Products
Return
ButtonGo:
;fetch the value of the checkbox variables
Gui, Submit
If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
If Open2 = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau\Dimensioneren"
;Now, if you want the gui to close after the actions are complete, then uncomment the next line - if you want the gui to remain, then don't uncomment it.
;Gui, Destroy
RETURN
GUIClose:
Gui, Destroy
Finally, in this scenario, you can execute either one or both of the checkbox commands. If you only want one or the other, you should use radio buttons instead.
For a hotkey to run the primary function:
#x::
gosub, ButtonGo
return
If you want the hotkey to open the GUI, then you need to wrap the GUI in a gosub and alter the hotkey's command.