OnEvent Confusion Version 2.0.2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
xroot
Posts: 41
Joined: 21 Jun 2019, 08:45

OnEvent Confusion Version 2.0.2

13 Jan 2023, 13:32

My confusion is Gui Close and Escape "OnEvent".

With (GuiObject).OnEvent "Close",ExitApp

I get an error message:
Parameter #1 of ExitApp requires a Number, but received a Gui.
010: txt.SetFont("s22 cnavy bold","Times New Roman")
012: win.Show("w500 h200")
013: Exit

ExitApp is a built-in function which on the docs say the callback can be a built-in.
Using fat arrow (GuiObject).OnEvent "Close",(*)=>ExitApp works without errors.
Even without a Close OnEvent my Gui closes. WHY?

With (GuiObject).OnEvent "Escape",(*)=>ExitApp not working, no error message.

What am I missing, thanks for any info on this issue.

My testing Gui:

Code: Select all

win := Gui(,"Test OnEvent Status")
win.BackColor := "Blue"

;win.OnEvent "Close",ExitApp          ;THIS IS NOT WORKING WITH ERROR MSG
;win.OnEvent "Close",(*)=>ExitApp   ;THIS WORKS
win.OnEvent "Escape",(*)=>ExitApp  ;THIS IS NOT WORKING

txt := win.AddText("center x50 y50 w400 h100 BackgroundAqua",Format("AHK Version={}`nOS Version={}`n{}",A_AhkVersion,A_OSVersion,(A_PtrSize=8?" 64Bit":" 32Bit")))
txt.SetFont "s22 cnavy bold","Times New Roman"

win.Show "w500 h200"

;Esc::ExitApp           ;THIS WORKS
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: OnEvent Confusion Version 2.0.2

13 Jan 2023, 13:41

Code: Select all

#Requires AutoHotkey v2.0
go := (*) => ExitApp()
win := Gui(,"Test OnEvent Status")
win.AddText('w230', 'Test')
win.Show
win.OnEvent('Escape', go)

Code: Select all

#Requires AutoHotkey v2.0
win := Gui(,"Test OnEvent Status")
win.AddText('w230', 'Test')
win.Show
win.OnEvent 'Escape', (*) => ExitApp()
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: OnEvent Confusion Version 2.0.2

13 Jan 2023, 14:29

Code: Select all

;win.OnEvent "Close",ExitApp          ;THIS IS NOT WORKING WITH ERROR MSG
ExitApp accepts one parameter, ExitCode Type: Integer. oneventclose passes one parameter, GuiObj Type: Gui https://www.autohotkey.com/docs/v2/lib/GuiOnEvent.htm#Close, so u get a type error

Code: Select all

;win.OnEvent "Close",(*)=>ExitApp   ;THIS WORKS
it "works" in the sense that it has no observable effects(to you). the gui is simply hidden. the lambda is returning a reference to the variable ExitApp, which contains the implementation of the function ExitApp, but at no point is the function ExitApp ever called. u have a knack for writing function call statements, but have a poor grasp of when the interpreter considers one such and when it doesnt https://www.autohotkey.com/docs/v2/Language.htm#function-call-statements ....it would have been easier if v2 did away with this faux command v1 syntax, but oh well too late for that now

Code: Select all

win.OnEvent "Escape",(*)=>ExitApp  ;THIS IS NOT WORKING
same as above, except Escape doesnt actually do anything to GUIs by default, so nothing observable happens

Code: Select all

;Esc::ExitApp           ;THIS WORKS
yes, because ure allowed the function call statement in this context

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: abraham71, Bing [Bot], zizanie13 and 64 guests