Gui Hotkeys problems

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
j123258
Posts: 7
Joined: 08 Dec 2017, 12:12

Gui Hotkeys problems

17 Jan 2018, 02:41

I wrote a simple GUI allowing users to define their own hotkeys,it can work,but when I changed the hotkey, I got two problems.

If I set a hotkey and press setting, it can work (when I press 2 , it shows 12)

Image

and then I let hotkey and used-key be null and press setting. (I want to reset the hotkey)

Image

But the button 2 could not be used, (when I press 2 ,there is nothing) ← Problem1
and then I set the other hotkey as follow

Image

I press 1 on the WordPad , it shows 13 ( it is correct),but press 2 it also shows 13 ← Problem2

I don't know how to solve these two problems,do you have any idea?

Code: Select all

#SingleInstance force
#NoEnv

Gui, Add, Text, x12 y30 w40 h20 , Hotkey1
Gui, Add, Hotkey, x52 y28 w40 h20 vUserInput1,
Gui, Add, Text, x95 y30 w40 h20,UseKey
Gui, Add, Hotkey, x140 y28 w40 h20 vUserInput2, 
Gui, Add, Hotkey, x180 y28 w40 h20 vUserInput3, 
Gui, Add, Hotkey, x220 y28 w40 h20 vUserInput4, 
Gui, Add, Hotkey, x260 y28 w40 h20 vUserInput5,  
Gui, Add, Hotkey, x300 y28 w40 h20 vUserInput6, 
Gui,font,s15
Gui, Add, Button, x12 y60 w120 h30 gSetting, Setting
Gui,Show

Setting:                    
{
GuiControlGet, UserInput1
GuiControlGet, UserInput2
GuiControlGet, UserInput3
GuiControlGet, UserInput4
GuiControlGet, UserInput5
GuiControlGet, UserInput6
Gui,submit,Nohide
}

if %UserInput1%
    Hotkey,$%UserInput1%,SendMe

SendMe:
SendInput,%UserInput2%
sleep 30
SendInput,%UserInput3%
sleep 30
SendInput,%UserInput4%
sleep 30
SendInput,%UserInput5%
sleep 30
SendInput,%UserInput6%
return

GuiClose:
ExitApp
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Gui Hotkeys problems

17 Jan 2018, 12:55

Hi j123258,


Try the following (see the comments alongside the source):

Code: Select all

#SingleInstance force
#NoEnv
#Warn

A_LastHotkey := "" ;  a variable used in the 'setting' subroutine' below

Gui, Margin, 5, 5
Gui, Add, Text, Section x12 y30 w40 h20, Hotkey1 ; use a section to position controls starting each time a new column
Gui, Add, Hotkey, ys-2 wp hp vuserInput0,
Gui, Add, Text, ys wp hp, UseKey
Loop, 5
	Gui, Add, Hotkey, ys-2 wp hp vuserInput%a_index%, ; the built-in variable 'a_Index' contains the number of the current loop iteration
Gui, Font, s15
Gui, Add, Button, xs y60 w120 h35 gsetting, &Setting
Gui, Show, AutoSize
return ; don't forget to put a return, otherwise the subroutine beneath this part -  the auto-execute part - of the script will executed.

setting:
Gui, Submit, Nohide
if (UserInput0 <> "") ; since the Hotkey can be the 0 which means 'false' use this instead
{
	if (A_LastHotkey) ; in order to disable any previous hotkey set by the script so that only one hotkey remains active
		Hotkey, %A_LastHotkey%, sendMe, Off ; disable the hotkey
	A_LastHotkey = $%userInput0%
    Hotkey, %A_LastHotkey%, sendMe, On ; active the hotkey
}
return ; end of the subroutine

sendMe:
Loop, 4
{
	; SendInput, % UserInput%a_index%
	MsgBox, % userInput%a_index% ; % forces an expression (see the documentation: https://www.autohotkey.com/docs/Variables.htm#Expressions#percent-space)
sleep 30
}
; SendInput, % UserInput5
MsgBox, % userInput5
return

GuiClose:
ExitApp
Hope this helps.
my scripts
j123258
Posts: 7
Joined: 08 Dec 2017, 12:12

Re: Gui Hotkeys problems

17 Jan 2018, 23:18

Thanks for your reply A_AhkUser

Problem 2 is be solved ,but the Problem1 doesn't.

Problem1 is when I set up a set of hotkeys,and then I want to reset the hotkey.
For example hotkey 2 ,used-Key 1 2 ,and then I reset the hotkey 2 (I let all hotkey and used-key be null and press setting in my code), so when I press 2 it will show 2 rather than null.

But I am still very grateful for your reply
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Gui Hotkeys problems

18 Jan 2018, 09:33

Hi j123258,
j123258 wrote:[...] I want to reset the hotkey. For example hotkey 2 ,used-Key 1 2 ,and then I reset the hotkey 2 (I let all hotkey and used-key be null and press setting in my code), so when I press 2 it will show 2 rather than null.
If I understand correctly you can add and else statement in your Setting subroutine which will actually disable the last hotkey set by the script if the first hotkey control is empty:

Code: Select all

#NoEnv
#Warn
SendMode Input
#SingleInstance force
; Windows 8.1 64 bit - Autohotkey v1.1.27.04 32-bit Unicode

A_LastHotkey := "" ;  a variable used in the 'setting' subroutine' below

Gui, Margin, 5, 5
Gui, Add, Text, Section x12 y30 w40 h20, Hotkey1 ; use a section to position controls starting each time a new column
Gui, Add, Hotkey, ys-2 wp hp vuserInput0,
Gui, Add, Text, ys wp hp, UseKey
Loop, 5
	Gui, Add, Hotkey, ys-2 wp hp vuserInput%a_index%, ; the built-in variable 'a_Index' contains the number of the current loop iteration
Gui, Font, s15
Gui, Add, Button, xs y60 w120 h35 gsetting, &Setting
Gui, Show, AutoSize
return ; don't forget to put a return, otherwise the subroutine beneath this part -  the auto-execute part - of the script will executed.

setting:
Gui, Submit, Nohide
if (UserInput0 <> "") ; since the Hotkey can be the 0 which means 'false' use this instead
{
	if (A_LastHotkey) ; in order to disable any previous hotkey set by the script so that only one hotkey remains active
		Hotkey, %A_LastHotkey%, sendMe, Off ; disable the hotkey
	A_LastHotkey = $%userInput0%
    Hotkey, %A_LastHotkey%, sendMe, On ; active the hotkey
} else { ; otherwise, if the hotkey control is null...
	Hotkey, %A_LastHotkey%, sendMe, Off ; disable the last set hotkey
}
return ; end of the subroutine

sendMe:
Loop, 4
{
	; SendInput, % UserInput%a_index%
	MsgBox, % userInput%a_index% ; % forces an expression (see the documentation: https://www.autohotkey.com/docs/Variables.htm#Expressions#percent-space)
sleep 30
}
; SendInput, % UserInput5
MsgBox, % userInput5
return

GuiClose:
ExitApp

my scripts
j123258
Posts: 7
Joined: 08 Dec 2017, 12:12

Re: Gui Hotkeys problems

18 Jan 2018, 12:48

Thanks for your help ! A_AhkUser
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Gui Hotkeys problems

19 Jan 2018, 11:39

I'm glad to help! :)
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Lamron750, mikeyww and 219 guests