How to avoid a hotkey being press more than twice Topic is solved
How to avoid a hotkey being press more than twice
I assign a hotkey to create and show a GUI, I want when that GUI is display that automatically disable that hotkey until that GUI is destroy.
Last edited by songdg on 01 Apr 2024, 21:19, edited 2 times in total.
Re: How to avoid a hotkey being press more than twice
Create a conditional hotkey using #HotIf?
Code: Select all
myGui := Gui()
#HotIf !WinActive(myGui)
F1::myGui.Show("w100 h100")
#HotIf
Re: How to avoid a hotkey being press more than twice
Thanks, if that GUI is also created through that hotkey.ntepa wrote: ↑01 Apr 2024, 21:12Create a conditional hotkey using #HotIf?
Code: Select all
myGui := Gui() #HotIf !WinActive(myGui) F1::myGui.Show("w100 h100") #HotIf
Re: How to avoid a hotkey being press more than twice
If the hotkey also creates the gui, try this:
Code: Select all
F1::{
static myGui
myGui := Gui()
myGui.Show("w100 h100")
Hotkey("F1", "Off") ; Disable the current hotkey
HotIfWinNotExist "ahk_id" myGui.hwnd
; Create a new hotkey that shows the gui if it's not showing
Hotkey("F1", (*) => myGui.Show("w100 h100"), "On")
HotIf
}
Re: How to avoid a hotkey being press more than twice
Thanks it works, but there's another problem, I want to add a button for submitting purpose, if click that button it will destroy the gui and also restore the hotkey. I try to mimic your example in MyBtn_Click, but that dosen’t work.ntepa wrote: ↑01 Apr 2024, 21:40If the hotkey also creates the gui, try this:Code: Select all
F1::{ static myGui myGui := Gui() myGui.Show("w100 h100") Hotkey("F1", "Off") ; Disable the current hotkey HotIfWinNotExist "ahk_id" myGui.hwnd ; Create a new hotkey that shows the gui if it's not showing Hotkey("F1", (*) => myGui.Show("w100 h100"), "On") HotIf }
Code: Select all
#Requires AutoHotkey v2.0
F1::{
static myGui
myGui := Gui()
MyBtn := myGui.Add("Button", "Default w80", "F1_test")
MyBtn.OnEvent("Click", MyBtn_Click) ; Call MyBtn_Click when clicked.
MyBtn_Click(*)
{
Saved := myGui.Submit()
myGui.Destroy
HotIfWinNotExist "ahk_id" myGui.hwnd
Hotkey("F1", (*) => myGui.Destroy, "On")
HotIf
}
myGui.Show("w100 h100")
Hotkey("F1", "Off") ; Disable the current hotkey
HotIfWinNotExist "ahk_id" myGui.hwnd
; Create a new hotkey that shows the gui if it's not showing
Hotkey("F1", (*) => myGui.Show("w100 h100"), "On")
HotIf
}
Re: How to avoid a hotkey being press more than twice
Why destroy the gui instead of letting it hide?
Code: Select all
#Requires AutoHotkey v2.0
F1::{
static myGui
myGui := Gui()
myBtn := myGui.Add("Button", "Default w80", "F1_test")
myBtn.OnEvent("Click", MyBtn_Click)
myGui.Show("w100 h100")
Hotkey("F1", "Off")
HotIfWinNotExist "ahk_id" myGui.hwnd
Hotkey("F1", (*) => myGui.Show("w100 h100"), "On")
HotIf
MyBtn_Click(*) {
saved := myGui.Submit()
}
}
Re: How to avoid a hotkey being press more than twice
Because there're RadioGroups in my Gui, I want to reset it when click that button.ntepa wrote: ↑02 Apr 2024, 14:15Why destroy the gui instead of letting it hide?Code: Select all
#Requires AutoHotkey v2.0 F1::{ static myGui myGui := Gui() myBtn := myGui.Add("Button", "Default w80", "F1_test") myBtn.OnEvent("Click", MyBtn_Click) myGui.Show("w100 h100") Hotkey("F1", "Off") HotIfWinNotExist "ahk_id" myGui.hwnd Hotkey("F1", (*) => myGui.Show("w100 h100"), "On") HotIf MyBtn_Click(*) { saved := myGui.Submit() } }
Re: How to avoid a hotkey being press more than twice
Code: Select all
#Requires AutoHotkey v2.0
F1::{
static myGui := CreateGui()
myGui.Show()
Hotkey "F1", "Off"
HotIfWinNotExist "ahk_id" myGui.hwnd
Hotkey "F1", (*) => myGui.Show(), "On"
HotIf
CreateGui() {
local g
g := Gui()
g.Add('Radio', 'vMyRadioGroup', 'Option 1')
g.Add('Radio', '', 'Option 2')
g.Add('Radio', '', 'Option 3')
g.Add('Button', 'Default w80', 'F1_test').OnEvent('Click', MyBtn_Click)
return g
}
MyBtn_Click(*) {
values := myGui.Submit()
MsgBox('Selected option ' values.MyRadioGroup)
myGui.Destroy()
; Reset the gui
myGui := CreateGui()
}
}
- Noitalommi_2
- Posts: 287
- Joined: 16 Aug 2023, 10:58
Re: How to avoid a hotkey being press more than twice Topic is solved
Hi.
Maybe I'm overlooking something, but shouldn't this be sufficient for the task?
Maybe I'm overlooking something, but shouldn't this be sufficient for the task?
Code: Select all
#Requires AutoHotkey 2.0
#SingleInstance
#HotIf !WinExist("MyGuiName")
F1:: {
MyGui := Gui(,"MyGuiName")
MyGui.AddButton(, "Destroy") .OnEvent("Click", GuiDestroy)
MyGui.Show("w200")
GuiDestroy(*) {
MyGui.Destroy()
}
}
#HotIf
Re: How to avoid a hotkey being press more than twice
Thanks, much appreciated for your helpntepa wrote: ↑02 Apr 2024, 21:57Code: Select all
#Requires AutoHotkey v2.0 F1::{ static myGui := CreateGui() myGui.Show() Hotkey "F1", "Off" HotIfWinNotExist "ahk_id" myGui.hwnd Hotkey "F1", (*) => myGui.Show(), "On" HotIf CreateGui() { local g g := Gui() g.Add('Radio', 'vMyRadioGroup', 'Option 1') g.Add('Radio', '', 'Option 2') g.Add('Radio', '', 'Option 3') g.Add('Button', 'Default w80', 'F1_test').OnEvent('Click', MyBtn_Click) return g } MyBtn_Click(*) { values := myGui.Submit() MsgBox('Selected option ' values.MyRadioGroup) myGui.Destroy() ; Reset the gui myGui := CreateGui() } }
Re: How to avoid a hotkey being press more than twice
Thank you very much, your solution is much simple and elegantNoitalommi_2 wrote: ↑02 Apr 2024, 22:57Hi.
Maybe I'm overlooking something, but shouldn't this be sufficient for the task?
Code: Select all
#Requires AutoHotkey 2.0 #SingleInstance #HotIf !WinExist("MyGuiName") F1:: { MyGui := Gui(,"MyGuiName") MyGui.AddButton(, "Destroy") .OnEvent("Click", GuiDestroy) MyGui.Show("w200") GuiDestroy(*) { MyGui.Destroy() } } #HotIf
Who is online
Users browsing this forum: Bing [Bot], TAC109 and 19 guests