OnExit not triggered when script contains #InstallKeybdHook

Get help with using AutoHotkey and its commands and hotkeys
joefiesta
Posts: 72
Joined: 24 Jan 2016, 13:54

OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 13:44

In the following, the OnExit routine is not triggered by the EXIT command:

Code: [Select all] [Download] GeSHi © Codebox Plus

#InstallKeybdHook
OnExit, ExitRoutine
exit
ExitRoutine:
msgbox exiting test....
exitapp


However, remove the #InstallKeybdHook directive and it works just fine. I can find no documentation about #InstallKeybdHook or OnExit being related.

thank you

Joe Petree
TheDewd
Posts: 831
Joined: 19 Dec 2013, 11:16
Location: USA

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 14:28

You are not calling the ExitApp command outside of the ExitRoutine label.

This directive (#InstallKeybdHook) also makes a script persistent, meaning that ExitApp should be used to terminate it.
User avatar
derz00
Posts: 404
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 18:46

I think people should be slower to post things in Bug Reports. But maybe the moderators don't mind.
try it and see
...
joefiesta
Posts: 72
Joined: 24 Jan 2016, 13:54

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 20:31

I spent 2 hours working on this before I discovered the extremely subtle relationship between EXIT, ONEXIT, a persistent script and #InstallkeybdHook. I read ALL the documentation about each. thoroughly. The DEWD did point me in the right direction to understanding the issue. (However, I DID READ that quote and believed that my code should have made my Exitapp execute.)

Yes, this is not a bug. and should be moved out of the bug forum. That is not to say, however, that I should not have posted the issue. I must also add that I searched both the old and new AHK forums for help on this issue. I found NOTHING even similar to my problem. Having used AHK for about 7 years now, I usually find that my difficulty in understanding comes from the difficulty of wading through the documentation. I feel this is entirely about the idiosyncratic way EXIT works in this context, and just wish there had been one small note about that.

Thank you for you input DEWD. As to the other respondent, I think you were just wasting your time being condescending.
User avatar
derz00
Posts: 404
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: OnExit not triggered when script contains #InstallKeybdHook

04 Aug 2017, 12:21

I apologize for my condescending attitude. And now I also realize that I was wasting my time posting that. I should have thought once or twice more before I hit submit. Now I must take more time to undo the damage done. I do find it amusing/annoying the way so many people post things in bug reports when they have no idea how it is actually supposed to work. But reading your second post, I see you did your homework, and the documentation itself can receive the blame. I guess my own attitude of being smart about AHK (which I undoubtedly am not) is to blame for that post.

Thanks for not being too polite to correct me!
try it and see
...
lexikos
Posts: 5686
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: OnExit not triggered when script contains #InstallKeybdHook

27 Dec 2017, 20:52

I have been going through old bug reports, and have moved this topic to Ask for Help.
I found NOTHING even similar to my problem.
Perhaps it's not a common mistake.
I feel this is entirely about the idiosyncratic way EXIT works in this context, and just wish there had been one small note about that.
Exactly what would you add to the documentation?

I would have thought that the presence of a tray icon would give away the fact that the script had not exited. It would be even more obvious if the script was single-instance, since a second attempt to run the script would tell you it is already running. If it is not exiting, there is no reason for OnExit to be called.

It seems that you assumed Exit would cause the entire script to exit; but why? I think the difference between Exit and ExitApp is already quite clearly documented. I don't understand why anyone would use Exit if the intention is to unconditionally exit the script.
ExitApp wrote:Terminates the script unconditionally.

Exit wrote:Exits the current thread or (if the script is not persistent and contains no hotkeys) the entire script.

Remarks

If the script has no hotkeys, isn't persistent, and hasn't requested the Num/Scroll/CapsLock key(s) to be kept AlwaysOn or AlwaysOff, it will terminate immediately when Exit is encountered (except if it has an OnExit subroutine).

Otherwise, the Exit command terminates the current thread. In other words, the stack of subroutines called directly or indirectly by a menu, timer, or hotkey subroutine will all be returned from as though a Return were immediately encountered in each. If used directly inside such a subroutine -- rather than in one of the subroutines called indirectly by it -- Exit is equivalent to Return.

Use ExitApp to completely terminate a script that is persistent or contains hotkeys.

Return to “Ask For Help”

Who is online

Users browsing this forum: BBomb007, Bing [Bot], boiler, n1ce0n3, sloepoke and 49 guests