How to avoid a hotkey being press more than twice Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

How to avoid a hotkey being press more than twice

01 Apr 2024, 21:07

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.
ntepa
Posts: 436
Joined: 19 Oct 2022, 20:52

Re: How to avoid a hotkey being press more than twice

01 Apr 2024, 21:12

Create a conditional hotkey using #HotIf?

Code: Select all

myGui := Gui()

#HotIf !WinActive(myGui)

    F1::myGui.Show("w100 h100")

#HotIf
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: How to avoid a hotkey being press more than twice

01 Apr 2024, 21:23

ntepa wrote:
01 Apr 2024, 21:12
Create a conditional hotkey using #HotIf?

Code: Select all

myGui := Gui()

#HotIf !WinActive(myGui)

    F1::myGui.Show("w100 h100")

#HotIf
Thanks, if that GUI is also created through that hotkey.
ntepa
Posts: 436
Joined: 19 Oct 2022, 20:52

Re: How to avoid a hotkey being press more than twice

01 Apr 2024, 21:40

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
}
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: How to avoid a hotkey being press more than twice

02 Apr 2024, 04:23

ntepa wrote:
01 Apr 2024, 21:40
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
}
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.

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
}
ntepa
Posts: 436
Joined: 19 Oct 2022, 20:52

Re: How to avoid a hotkey being press more than twice

02 Apr 2024, 14:15

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()
    }
}
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: How to avoid a hotkey being press more than twice

02 Apr 2024, 20:49

ntepa wrote:
02 Apr 2024, 14:15
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()
    }
}
Because there're RadioGroups in my Gui, I want to reset it when click that button.
ntepa
Posts: 436
Joined: 19 Oct 2022, 20:52

Re: How to avoid a hotkey being press more than twice

02 Apr 2024, 21:57

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()
    }
}
User avatar
Noitalommi_2
Posts: 287
Joined: 16 Aug 2023, 10:58

Re: How to avoid a hotkey being press more than twice  Topic is solved

02 Apr 2024, 22:57

Hi.

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
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: How to avoid a hotkey being press more than twice

03 Apr 2024, 04:09

ntepa wrote:
02 Apr 2024, 21:57

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()
    }
}
Thanks, much appreciated for your help :bravo:
songdg
Posts: 616
Joined: 04 Oct 2017, 20:04

Re: How to avoid a hotkey being press more than twice

03 Apr 2024, 04:11

Noitalommi_2 wrote:
02 Apr 2024, 22:57
Hi.

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
Thank you very much, your solution is much simple and elegant :thumbup:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], TAC109 and 19 guests