How to make it so you could change the hotkey in the GUI? - AutoHotkey - autohotkey

Code:
Gui, Add, Text,, ------------------------------------------Key Delay------------------------------------------
Gui, Add, Edit, w300 vKeyDelay, 100
Gui, Add, Text,, ------------------------------------------Key Input------------------------------------------
Gui, Add, Edit, R10 w300 vKeyPlayer
Gui, Add, Text,, ------------------------------------------Key Start------------------------------------------
Gui, Add, Edit, w300 vStartKey, F2
Gui, Show
F2::
!F2::
Gui, Submit, Nohide
SetKeyDelay, %KeyDelay%
Send, %KeyPlayer%
return
GuiClose:
ExitApp
The start key is set to F2, i want to make it so people are able to change it to whatever, (F1, F2, F3, A, B, C, 1-10, etc)
How to make it so you could change the hotkey in the gui?

You'd use the Hotkey command to create a hotkey during runtime.
And to choose the hotkey in the gui, the easiest (but not best) option would be the hotkey control.
It's surely the easiest one that's also convenient for the end user, but it doesn't support anything beyond just regular hotkeys. For better approaches, you'd need a custom one.
This was the first custom one I found with a Google search. Haven't used it myself, but it might be good stuff.
A very easy, but powerful, custom one you could also use is just a Edit control. Works very well if you expect your end users to be smart enough to type stuff like !F1, +#k, d & o, or whatever.
Anyway, I'll demonstrate here the usage of the built in hotkey control. Stop reading now if you want to figure it out yourself.
First, create the gui and associate a variable and a g-label to the hotkey control.
Though, I'm going to use a function instead of a label, I don't like writing legacy AHK.
Gui, Add, Hotkey, % "x50 y25 w90 h30 vChosenHotkey gHotkeyChanged"
Gui, Show, % "w200 h100"
Return
Then the g-label HotkeyChanged needs to be defined, and I'll use a function instead of a label, as said above.
HotkeyChanged()
{
global ChosenHotkey
Gui, Submit, NoHide
Hotkey, % ChosenHotkey, MyHotkey, On
}
And when using a function, you have to worry about scopes, which is why global ChosenHotkey is specified.
There I'm telling the function that I'll use a variable defined outside of its scope.
If scopes are something you don't yet know of, and don't yet want to to learn about them, you can write legacy AHK and use a label and forget about all this.
To learn about scopes in programming in general, you can probably find something good from Google.
And to learn about them in AHK specifically, I have a previous answer about them here and here's the relevant documentation page.
Then I get the make the script update its associated variables with Gui, Submit, you already seem to know about this.
And then I get to the Hotkey command.
First parameter takes the hotkey to use, that's stored in the ChosenHotkey variable.
Second parameter a label/function name, or a function object. I'll use a function name MyHotkey.
And in the third parameter On is specified to turn the hotkey on and possibly replace any previous any previous hotkey that was in its place.
Then the function or label MyHotkey needs to be defined:
MyHotkey()
{
MsgBox, % "Hotkey pressed!"
}
And that's it.
If you want to save the previously used hotkey and then use it again when the script is restarted, there are many options all of which basically just boil down the idea of saving the hotkey to some file.
Here's the full script:
Gui, Add, Hotkey, % "x50 y25 w90 h30 vChosenHotkey gHotkeyChanged"
Gui, Show, % "w200 h100"
return
HotkeyChanged()
{
global ChosenHotkey
Gui, Submit, NoHide
Hotkey, % ChosenHotkey, MyHotkey, On
}
MyHotkey()
{
MsgBox, % "Hotkey pressed!"
}
GuiClose()
{
ExitApp
}

Related

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.

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

Reuse a GUI Input in AutoHotKey

/**********************************
GUI
*/
Gui, Add, Edit, x12 y7 w163 h19 vLoopCount, Loopcount
Gui, Add, Edit, x12 y26 w163 h19 vCheckCount, ErrorCheck
Gui, Add, Button, x12 y55 w76 h19 gStart, Start
Gui, Show, w194 h80, Test
return
Start:
gui, submit, nohide
count := LoopCount
check_count_basic := CheckCount
gui, hide
check_count := check_count_basic
VarSetCapacity(vLoopCount,0)
VarSetCapacity(vCheckCount,0)
/**********************************
GUI
*/
Is a tiny part of My ahk script.
See, I not have 'ExitApp' method in Script. because i want use script without "Re-Run Script".
but, If i try second starting my Script.
"The Same variable cannot be used for more than one control"
I already Know Ahk's variable is not reusable. So i try using 'VarSetCapacity(vLoopCount,0)'. but isn't work what i think.
How can i do? It is really one way "use 'ExitApp' method"?
If you want to reuse the exact same GUI within your code, you only need it in your code once. Use the GuiControl command to update specific controls. For instance, should you need to clear out your "LoopCount" variable for a different set of code, you could do something like this:
LoopCount := 0
GuiControl ,, LoopCount , %LoopCount%
Gui , Show
The VarSetCapacity function is used for setting how much memory your variable can use. In your case, even though you're clearing it out, it still exists as a name and cannot be used for more than one control since AHK uses this name as one means of identifying it.
From your code, it looks like you already understand how to use Gui , Hide and Gui , Show. I think that's really all you need to accomplish what you're asking.

Paste into custom GUI with AHK

I have been working on this for a while and I have no idea how to fix this issue. I want to create a custom GUI in AutoHotKey (AHK), I would post ont he AHK Forums but I haven't been able to get my account to work so I am posting here (sorry if this is the wrong place). The ideal state is that I can paste in a list of indiscriminate length from a list, it is almost always return delimited, see the picture below. I would be happy with pasting 10 items in. I have built the GUI but I can not paste the values in with the shortcut Ctrl+v. All that happens is the first value goes into the first cell and I cannot figure out how to get the rest to paste in.
I need to be able to read the values into an array in the AHK when I click continue. Thanks for your help in advance. Below is my code to create the GUI.
Gui, Add, Text,, Please add the List that you want (10 Max at once)
Gui Add, Edit, vButton1,
Gui Add, Edit, vButton2,
Gui Add, Edit, vButton3,
Gui Add, Edit, vButton4,
Gui Add, Edit, vButton5,
Gui Add, Edit, vButton6,
Gui Add, Edit, vButton7,
Gui Add, Edit, vButton8,
Gui Add, Edit, vButton9,
Gui Add, Edit, vButton0,
Gui Add, Button, x200 y270 w88 h26 vButton02 gGoCont, Continue
Gui Add, Button, x290 y270 w88 h26 vButton03 gGoQuit, Cancel
Gui Show
return
GoCont:
{
MsgBox %Button1%
MsgBox %Button2%
}
return
GoQuit:
Gui Destroy
return
If you can stand a txt file with one name per line, called "names.txt" in the same folder as your ahk script, try something like this:
Add this to the top (it reads in your names.txt file one line at a time):
Loop, Read, names.txt
x%A_Index% := A_LoopReadLine
START EDIT (per comments):
Alternatively, if you already copied to the clipboard the several names from a spreadsheet or website table or other list, then put it this way:
Loop, parse, Clipboard, `n, `r
x%A_Index% := A_LoopField
Either way,
END EDIT
Then, replace all 10 of your edit box lines with these two lines:
Loop, 10 ; or more?
Gui Add, Edit, vButton%A_Index%, % x%A_Index%
The rest is just as you had it.
Let us know, Have fun,
Big thanks to #PGilm
Gui, PasteGUI:Add, Text,, Please add the Names that you want to Process.
Counter := 0
Loop, parse, Clipboard, `n, `r
{
x%A_Index% := A_LoopField
Counter++
}
Counter--
Loop, %Counter% ; Dynamic List length
Gui PasteGUI:Add, Edit, vButton%A_Index%, % x%A_Index%
Gui PasteGUI:Add, Button, x200 y270 w88 h26 vButton02 gGoCont Default, Continue
Gui PasteGUI:Add, Button, x290 y270 w88 h26 vButton03 gGoQuit, Cancel
Gui, PasteGUI:Show
}
Return
GoCont:
{
Loop, %Counter%
{
CODE TO PROCESS MY EACH NAME
}
MsgBox Done!
Gui Destroy
}
Return
GoQuit:
Gui Destroy
Return
Lastly if I want to add a keyboard shortcut to work then I mapped in one where I put the below line at the top of the code
PasteIn:
{
And then close the bracket at the end of the code and then add the shortcut. (the below can be added to the bottom of the code to work) this uses the Ctrl+v keyboard shortcut.
}
^v:: GoTo, PasteIn

autohot key using Text Box and a button

Need a little help with a text box and a button..
I have been trying to experiment with "text box" and a "button"..but am not able to do it... I need to take the "values" from the "textbox" field and append it to the button action. i.e., whatever the input is given by the user... need to append it to a "URL"
Example:
If the user is types in "login" as input in the text field, need to take this input and append it to "http://www.google.com/login"
Before action:
http://google.com
AfterAction:
http://google.com/login
Kindly help
Here is my Code:
Gui, Add, Edit, x162 y80 w140 h30 vUserInput, Type here
Gui, Add, Button, x182 y130 w100 h30 gAction, %"Action1" Action1:=http://www.google.om/
; Generated using SmartGUI Creator 4.0
Gui, Show, x127 y87 h379 w479, New GUI Window
Return
Action1:
Gui, Submit, NoHide
guiControlGet, txtVar,, UserInput
guiControl,, UserInput, %( txt++Action1 )
GuiClose:
ExitApp
return
Thanks and cheers.
Okay, you have a few misconceptions and a LOT of errors.
The use of := -vs- % is one of your misunderstandings.
Also, you neglected to put gAction1 as the goto label for your button. You had gAction, but there was no sub called Action.
Another problem is that you have not placed return at the end of your Action1 label.
That means that after the action is completed, the script will continue on to end the script...
You should really read the AutoHotkey Documentation! It will save you a lot of time and headache. Also, you should look at some simple examples.
However, try the script below. Whatever you type into the box, after you push the button, will be appended to the url.
;Now, it may be easier to define your re-usable variable here like this:
;myurl = http://www.google.com/
myurl := "http://www.google.com/" ;this is exactly same as the previous line
Gui, Add, Edit, x162 y80 w300 h30 r1 vUserInput, Type here
Gui, Add, Button, x182 y130 w100 h30 gAction1, Action1 ;The last parameter is JUST the text on the button
;If you wanted to use a variable for the name instead of straight text, you would do this:
;Gui, Add, Button, x182 y130 w100 h30 gAction, %myurl%
;But what you were trying to do is to *assign* a variable instead of *using* a variable - that is, you CAN'T use action1:=value as the variable name
Gui, Show, x127 y87 h379 w479, New GUI Window
Return
Action1: ;the name of this label is the same as the g-action of your button except without the "g" part.
Gui, Submit, NoHide
guiControlGet, UserInput ;you can simply use the edit control's v-name as the output variable
thisurl = %myurl%%UserInput%
guiControl,,UserInput, %thisurl% ;now set the url back to the control
return ;if you don't put return here, the script will continue on and run the GuiClose label...
GuiClose:
ExitApp
return ;you can have this, but you don't need it here since the script will have ended before it gets run anyway.