GUI and Hotkey!!! Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gandalfgarfield
Posts: 29
Joined: 20 Jun 2020, 22:27

GUI and Hotkey!!!

17 Aug 2020, 20:06

Hello, I am trying to do a GUI for the script I am working on. I'm very new to coding and have no background with any other coding language. I've only done basic "hello world" back in college so please make it simple for me.

So I have a script that will I wanna get triggered by a Hotkey that will be set by the GUI that I will make.
I've already tried using InputBox() to try changing the Hotkeys and it worked but in GUI, I don't get how it won't work.

Sample script I did using the InputBox

Code: Select all

InputBox, var, Input
Hotkey, %var%, Myscript
Myscript:
Send, It's working
return
That script works fine whenever I change the value if the variable.

Script with GUI

Code: Select all

Gui, New, Toolwindow, My GUI
Gui, -System
Gui, Add, Edit, vMyvar
Gui, Add, Button, gset, Set
Gui, show, My GUI

Set:
Gui, Submit, Nohide
Gui, add, text, %Myvar%

Hotkey, %Myvar%, Myscript
Myscript:
Send THIS DOESNT WORK :(
Return
I don't understand why it isn't working. I've tried different stuffs like using GuiControlGet cause I thought maybe it has to do something with how the GUI declare it's variable but still it doesn't work.
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: GUI and Hotkey!!!

17 Aug 2020, 21:14

Code: Select all

Gui, New, Toolwindow, My GUI
Gui, Add, Edit, vMyvar
Gui, Add, Button, gset, Set
Gui, show,, My GUI
return

Set:
	Gui, Submit, Nohide
	Hotkey, %Myvar%, Myscript
return

Myscript:
	MsgBox THIS WORKS :)
Return
gandalfgarfield
Posts: 29
Joined: 20 Jun 2020, 22:27

Re: GUI and Hotkey!!!

17 Aug 2020, 23:27

Thank you it works but there's an error right after i opened the script, can you correct this for me
Here is my code:

Code: Select all

Gui, New, Toolwindow, My GUI
Gui,  -SysMenu 
Gui, Font, s10, Verdana
Gui, add, text,x20 y20, Toggler
Gui, add, text,, Key
Gui, add, text,, Name
Gui, add, text,, Pass
Gui, Font, s10 wbold, Verdana
Gui, add, text,, Name
Gui, add, text,, Pass
Gui, Font, s10 w, Verdana
Gui, add, edit, vtoggler w40 x100 y17
Gui, add, edit, vkey wp 
Gui, add, edit, vname wp 
Gui, add, edit, vpass wp 
Gui, add, button,gSubmit x210 y20,&Set
Gui, add, button,gClose x210 y60,&Close
Gui, show,W270 H210, My GUI

Submit:
Gui, Submit, NoHide
Gosub, Set
Set:
Gui, Font, wbold s10 c000000, verdana
Gui, add, text,x160 y20, %toggler%
Gui, add, text,, %key%
Gui, Font, wbold s10 c008000, verdana
Gui, add, text,, %name%
Gui, Font, wbold s10 c0000FF, verdana
Gui, add, text,, %pass%
Hotkey, %toggler%, Auto
return

Close:
ExitApp
return

Auto:
Send send this output
return
so i want the texts I typed on the textbox to be written on the GUI right after i clicked the Set button

Edit: Also i just realized with you script. When i set the hotkey for example F1, press F1 and it sends the msgbox but when i set another key like F2, it still works but when i press F1 it still sends the message. I want only one key to work, just what i set last.
User avatar
Epialis
Posts: 858
Joined: 02 Aug 2020, 22:44

Re: GUI and Hotkey!!!

18 Aug 2020, 05:42

@gandalfgarfield

Not sure what's not working but I added an F9 hotkey to find the error.. it still worked though when I put the information in the box. But I removed this line... and it still works

Code: Select all

Hotkey, %toggler%, Auto
All the information I put in, was shown on the side in text. Not sure if that's how you wanted it... but thought I"d try anyway :) Be blessed.
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: GUI and Hotkey!!!  Topic is solved

18 Aug 2020, 06:45

See the comments in the code below:

Code: Select all

Gui, New, Toolwindow, My GUI
Gui,  -SysMenu 
Gui, Font, s10, Verdana
Gui, add, text,x20 y20, Toggler
Gui, add, text,, Key
Gui, add, text,, Name
Gui, add, text,, Pass
Gui, Font, s10 wbold, Verdana
Gui, add, text,, Name
Gui, add, text,, Pass
Gui, Font, s10 w, Verdana
Gui, add, edit, vtoggler w40 x100 y17
Gui, add, edit, vkey wp 
Gui, add, edit, vname wp 
Gui, add, edit, vpass wp 
Gui, add, button,gSubmit x210 y20,&Set
Gui, add, button,gClose x210 y60,&Close
Gui, show,W270 H210, My GUI
return ; need to end the auto-execute section with a return or it falls through to the code below (causing the error)

Submit:
if toggler ; only execute next line if a key has already been assigned
	Hotkey, %toggler%, Off ; turns off prior hotkey if one has been set
Gui, Submit, NoHide
Gosub, Set
Set:
Gui, Font, wbold s10 c000000, verdana
Gui, add, text,x160 y20, %toggler%
Gui, add, text,, %key%
Gui, Font, wbold s10 c008000, verdana
Gui, add, text,, %name%
Gui, Font, wbold s10 c0000FF, verdana
Gui, add, text,, %pass%
Hotkey, %toggler%, Auto
return

Close:
ExitApp
return

Auto:
Send send this output
return
See structure of a script in the AHK documentation regarding the auto-execute section.

By the way, it is generally better to use a a Hotkey control (Gui, Add, Hotkey) than an Edit control for capturing hotkeys. You might want to try that.
gandalfgarfield
Posts: 29
Joined: 20 Jun 2020, 22:27

Re: GUI and Hotkey!!!

18 Aug 2020, 20:18

@Epialis Thanks for the reply! I think i havent explained the problem well, Ive actually made a few adjustments. If you have the time please Try to run it, Ill explain the problem below after the script.

@boiler Thank you! It made the error go away but it still doesnt work the way i want it. Ive also used the GUI, Add, Hotkey. Thanks! Please try to run the script and ill explain my problem below.
Note: I will read the structure of script after posting this!!! thanks

Code: Select all

Gui, New, Toolwindow, My GUI
Gui,  -SysMenu 
Gui, Font, s10, Verdana
Gui, add, text,x20 y20, Toggler
Gui, add, text,, Key
Gui, add, text,, Name
Gui, add, text,, Pass
Gui, Font, s10 wbold, Verdana
Gui, add, text,, Name
Gui, add, text,, Pass
Gui, Font, s10 w, Verdana
Gui, add, Hotkey, vtoggler w40 x100 y17
Gui, add, Hotkey, vkey wp 
Gui, add, Edit, vname wp +Number
Gui, add, Edit, vpass wp +Number
Gui, add, button,gSubmit x210 y20,&Set
Gui, add, button,gClose x210 y60,&Close
Gui, show,W270 H210, My GUI
return ; need to end the auto-execute section with a return or it falls through to the code below (causing the error)

Submit:
if auto ; only execute next line if a key has already been assigned
	Hotkey, %toggler%, Off ; turns off prior hotkey if one has been set
Gui, Submit, NoHide
Gosub, Set
Set:
Gui, Font, wbold s10 c000000, verdana
Gui, add, text,x160 y20, %toggler%
Gui, add, text,, %key%
Gui, Font, wbold s10 c008000, verdana
Gui, add, text,, %name%
Gui, Font, wbold s10 c0000FF, verdana
Gui, add, text,, %pass%
Hotkey, %toggler%, Auto
return

Close:
ExitApp
return

Auto:
Msgbox, right
return

rshift::reload
rctrl::exitapp
So when i run the script. The previous error that shows up is gone but there are few problems that i encounter.

First for example, I use the Hotkey F1 for the toggle and click set, "F1" will show on the third column like its supposed to, when i press F1 it also works the Msgbox appears. Then i change it to F2 then click set, "F2" will show on the third colum, when F2 is pressed the msgbox will appear but if i press F1, Msgbox still appears, It shouldnt happen :(

Second Problem is when F1 or F2 is shown on the 3rd column then i try to put single letter hotkeys like, 1 or 2,
the 3rd column shows "11" or "12" or "22", :(

THANKS FOR ANSWERING!
gandalfgarfield
Posts: 29
Joined: 20 Jun 2020, 22:27

Re: GUI and Hotkey!!!

19 Aug 2020, 03:05

@boiler , so i just saw that you solved the problem of the previously assigned keyname not getting cleared after assigning new keyname with this code

Code: Select all

Submit:
if auto ; only execute next line if a key has already been assigned
	Hotkey, %toggler%, Off ; turns off prior hotkey if one has been set
Ive done research and it should really do the trick, but it doesnt :(
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: GUI and Hotkey!!!

19 Aug 2020, 03:13

I didn’t say if auto, I said if toggler, the name of the variable that contains the hotkey. Where did you get if auto from?
gandalfgarfield
Posts: 29
Joined: 20 Jun 2020, 22:27

Re: GUI and Hotkey!!!

19 Aug 2020, 17:56

@boiler OHHHHHHH yeah sorry i dont know where i got that too sorryyyyyyyyyy ill try it now

Edit: ITS WORKINGGGGGG THANKS!!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Bing [Bot], madensuyu1, peter_ahk and 337 guests