OnMessage with Empty Second Parameter Generates Error

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

OnMessage with Empty Second Parameter Generates Error

05 Jun 2015, 22:02

The OnMessage documentation says:
OnMessage(MsgNumber [, "FunctionName", MaxThreads])
;...
FunctionName
A function's name, which must be enclosed in quotes if it is a literal string, or a reference to a function. This function will be called automatically when the script receives MsgNumber. Omit this parameter to retrieve the name of the function currently monitoring MsgNumber (blank if none). Specify an empty string ("") or an empty variable to turn off the monitoring of MsgNumber.
However, omitting FunctionName or specifying an empty string both generate runtime errors when I try to use them. Am I doing something wrong? If not, how to I unregister for a message?

Code: Select all

Gui("TokenEntry: +LastFound")
OnMessage(0x201, "WM_LButtonDown")
GuiAddEntryHandle := WinExist()
WinWaitClose ahk_id %GuiAddEntryHandle%
OnMessage(0x201, "")
Randy31416
Posts: 58
Joined: 15 Jan 2014, 19:09

Re: OnMessage with Empty Second Parameter Generates Error

05 Jun 2015, 22:25

Can't really help, because your script works fine here (Win 7 64-bit and 64-bit AHK). I had to delete the first line ("Gui(...}") because that function is not present in your sample. The script then runs nicely.

I then added to your script a dummy function WM_LButtonDown (to match what you supply to OnMessage), and changed the second OnMessage call (the one with the empty second argument) to store its result in a variable. The value of the variable after the second call is then in fact "WM_LButtonDown", the name of the function currently monitoring the message. So I suspect you have a problem in you installation or possibly somewhere else in the larger script from which you have excised the sample.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: OnMessage with Empty Second Parameter Generates Error

05 Jun 2015, 23:48

@egocarib: I am very rarely motivated to update the v2 documentation. Usually I only update v2-changes, but I also usually forget to upload it. I have just done so; the section which explains your issue is:
OnMessage has been changed to treat function names the same way that function references are treated in v1.1.20. That is, OnMessage(x, "MyFunc") registers MyFunc for message x while still allowing other functions to monitor message x. The function must be unregistered using OnMessage(x, "MyFunc", 0), not OnMessage(x, ""), which is now an error. OnMessage(x) is also now an error. On failure, OnMessage throws an exception.
Source: v2-changes
OnMessage(x, "") is an error because there can be any number of functions monitoring the message, and it doesn't specify which one to remove.

You can also refer to the changelog here or on GitHub.

@Randy31416: egocarib is using AutoHotkey v2-alpha, hence the link to the v2 documentation on GitHub and the use of Gui() as a function.
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: OnMessage with Empty Second Parameter Generates Error

06 Jun 2015, 08:08

Thanks for the doc update - I usually check v2-changes too, but I didn't check the changelog - I'll keep an eye on that going forward.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot], Ineedhelplz, Spawnova and 340 guests