Use Enter button for submit, while working normally elsewhere - autohotkey

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.

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?

How to make it so you could change the hotkey in the GUI? - 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
}

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.

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

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.