How to detect keypress event and on a gui control in AutoHotkey v2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 00:39

I try to write 2 gui function. I search this topic on web and document but hard to find resource.
1. Press Escape key close GUI
2. Press Enter key search user input text (maybe I should use inputbox...) or appendfile. (in main_gui do nothing in search_gui search input text ...)

I already try something in below.

Code: Select all

; with bug
SetWorkingDir A_ScriptDir
#SingleInstance
; AutoHotkey v2
q::
{
Gui := GuiCreate()
Gui.Opt("+LastFound +AlwaysOnTop -SysMenu +ToolWindow -caption +Border +Owner")
Gui.BackColor := "EEAA99"
ref1 := Gui.Add("Edit", "r1 vMyEdit w180", "")
ref1.SetFont("cBlue")
Gui.Show()

ref1.OnEvent('Change', 'Findus')
Findus(GuiCtrlObj, Info) {
	if (GuiCtrlObj.Value == "key ") ; with space
	{
	gui_keyDetect()
	GuiCtrlObj.Gui.Destroy()
	}
	else if (GuiCtrlObj.Value == "exit")
	{
	GuiCtrlObj.Gui.Destroy()
	ExitApp()
	}
	else if (GuiCtrlObj.Value == "reload")
	{
		Reload
	}
	else if (GuiCtrlObj.Value == "search")
	{
	gui_search("search title")
	GuiCtrlObj.Gui.Destroy()
	}
	}
}

gui_search(guiTitle) {
myGui := GuiCreate()
myGui.AddText("", guiTitle)
ref1 := myGui.Add("Edit", "r1 vMyEdit w180", "")
myGui.Show()
ref1.OnEvent('Change', 'Findus')
Findus(GuiCtrlObj, Info) {
	; how to search when press enter
	userInput := GuiCtrlObj.Value
	; Run "http://www.google.com/search?q=" userInput
	}
}

gui_keyDetect() {
Gui2 := GuiCreate()
ref2 := Gui2.Add("Hotkey", "vChosenHotkey")
Gui2.Show()

ref2.OnEvent('Change', 'keyDetect')
keyDetect(GuiCtrlObj, Info) {
	; Esc::GuiCtrlObj.Gui.Destroy() ; didn't work
	if (GuiCtrlObj.Value = "{Escape}") ; didn't work
	{
		GuiCtrlObj.Gui.Destroy()
	}
	else if (GuiCtrlObj.Value = "Escape") ; didn't work
	{
		GuiCtrlObj.Gui.Destroy()
	}
	else if (GuiCtrlObj.Value = "Esc") ; didn't work
	{
		GuiCtrlObj.Gui.Destroy()
	}
	else if (GuiCtrlObj.Value = "w") ; works
	{
		msgbox("you press w key")
	}
}
}


Last edited by fwejifjjwk2 on 21 Apr 2020, 00:07, edited 3 times in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 02:56

just make context sensitive hotkeys

Code: Select all

#If WinActive(Gui.Hwnd)
Esc::Gui.Destroy()
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 03:08

@swagfag
Thanks. I spend so many time on this issue.
But how to detect Enter key in different gui run different function? e.g. searhing or appendfile.
But in ref2 GUI it can't choic given GUI. Should I use detect GUI exist or have orther method like v1's method.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 03:24

different guis should have different distinct hwnds, so that shouldnt be a problem

u can also check whether a certain control is focused, stuff like that
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 03:59

@swagfag
Thanks.But when GUI not active ESC key still try to do Gui.Destroy() and make some error.
I try to use different GUI hwnds but not working.
I use variable with state decide action.

Code: Select all

gui_sate := 1
#if (gui_sate == 1)
Esc::
{
	Gui.Destroy()
	gui_sate := 0
}
#If
I would think about use detect GUI ( not familiar with ) or another method close GUI (not use keybinding)
Last edited by fwejifjjwk2 on 20 Apr 2020, 04:29, edited 3 times in total.
User avatar
Ragnar
Posts: 613
Joined: 30 Sep 2013, 15:25

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 04:06

For number 1, you can use something like the following:

Code: Select all

Gui.OnEvent("Escape", (this) => this.Hide())
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 04:16

@Ragnar
Thanks. It works. Right now I use it close 3 different GUI works pretty fine.

Code: Select all

Gui.OnEvent("Escape", (this) => this.Hide()) ; in !q hotkey
Gui2.OnEvent("Escape", (this) => this.Hide()) ; in gui_keyDetect()
myGui.OnEvent("Escape", (this) => this.Hide()) ; in gui_search(guiTitle)
Last edited by fwejifjjwk2 on 20 Apr 2020, 12:22, edited 1 time in total.
User avatar
Ragnar
Posts: 613
Joined: 30 Sep 2013, 15:25

Re: How to detect keypress event and on a gui control in AutoHotkey v2  Topic is solved

20 Apr 2020, 04:45

For number 2: The edit control triggers the default button of the GUI if you press ENTER, so you can use something like the following:

Code: Select all

MyBtn := Gui.Add("Button", "Default Hidden")
MyBtn.OnEvent("Click", "MyBtn_Click")

MyBtn_Click(*)
{
    global ref1
    MsgBox ref1.Value
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 10:41

ure doing something wrong then.

Code: Select all

G := GuiCreate()
G.Show('w200 h200')

#If WinActive(G.Hwnd)
Esc::G.Destroy()
performs exactly as one would expect
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 12:19

@swagfag
In this script (show below). When script not active any GUI. Press Escape still active Gui.Destroy().
And when it under Gui.Show() needed add Return or when GUI shows up it will close automatic.

Code: Select all

; with bug
q::
{
Gui := GuiCreate()
Gui.Opt("+LastFound +AlwaysOnTop -SysMenu +ToolWindow -caption +Border +Owner")
Gui.BackColor := "EEAA99"
ref1 := Gui.Add("Edit", "r1 vMyEdit w180", "")
ref1.SetFont("cBlue")
Gui.Show()
Return
}
Return

; when GUI not show no Gui.Hwnd
3::msgbox(Gui.Hwnd)

#If WinActive(Gui.Hwnd)
Esc::Gui.Destroy()
#If

Last edited by fwejifjjwk2 on 21 Apr 2020, 00:06, edited 2 times in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 12:44

thats not what happens. if the gui(a pinkish edit box) isnt the active window, pressing Escape does nothing.
if the gui is the active window, pressing Escape destroys it(the pinkish gui)
And when it under Gui.Show() needed add Return or when GUI shows up it will close automatic.
so much is a given. hotkey subroutines execute top to bottom until the first (explicit/implicit) return is encountered
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 12:49

swagfag wrote:
20 Apr 2020, 12:44
thats not what happens. if the gui(a pinkish edit box) isnt the active window, pressing Escape does nothing.
I know it will from top to buttom but I think maybe keybinding shouldn't detect b because it seens always true.
In my post script will show error. Your post code in my computer works fine.
In my computer Escape will try to close GUI even GUI not active. Maybe my AutoHotkey v2 complie have some problem?

Error message

Code: Select all

Error: The Gui is destroyed.
Line#
002: SetWorkingDir(A_ScriptDir)
Last edited by fwejifjjwk2 on 21 Apr 2020, 00:06, edited 7 times in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 12:56

i couldnt possibly tell. i ran it with the latest version available - a108. i can only suggest u try doing the same
ur script, as is
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 13:13

thats on repeated keypresses after the gui has been already destroyed once and not yet recreated. u can(in order of most recommended to least recommended):
  • not destroy/create the gui every time, but hide/show it
  • store the hwnd in a variable instead

    Code: Select all

    #if winactive(hwnd)
    ...
    ...
    gui := guicreate()
    hwnd := gui.hwnd
    ...
  • catch the exception

    Code: Select all

    #if trywinactive(gui.hwnd)
    
    trywinactive(args*) {
    	try
    		return winactive(args*)
    	catch
    		return false
    }
i vote for #1
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: How to detect keypress event and on a gui control in AutoHotkey v2

20 Apr 2020, 13:17

@swagfag
It works. I got what you mean. In this method we not active GUI won't active keybinding.

Code: Select all

q::
{
Gui := GuiCreate()
Gui.Opt("+LastFound +AlwaysOnTop -SysMenu +ToolWindow -caption +Border +Owner")
Gui.BackColor := "EEAA99"
ref1 := Gui.Add("Edit", "r1 vMyEdit w180", "")
ref1.SetFont("cBlue")
hwnd1 := Gui.hwnd
Gui.Show()
}
Return

#if winactive(hwnd1)
hwnd1 := Gui.hwnd
Esc::ToolTip("active")
#if

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Johncoool and 40 guests