How to remove an onmessage?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kauan014
Posts: 55
Joined: 18 Feb 2021, 20:03

How to remove an onmessage?

09 Jun 2022, 15:25

if i call onmessage with the same window message twice, binding each one to different functions

Code: Select all

OnMessage(0x200 , "func1")
OnMessage(0x200 , "func2")
when i call:

Code: Select all

OnMessage(0x200 , "")
it unregister both functions, i would like to handle different things inside of each function
my idea was to unregister the message on a class destructor when its not need anymore

is possible to remove one of them without disabled the second ?
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: How to remove an onmessage?

09 Jun 2022, 18:36

You could keep all the function names in an array, and call a function that clears all of them then binds any it finds in the array. The destruction would then remove the appropriate array item then call the function that removes all then rebinds what remains in the array.
kauan014
Posts: 55
Joined: 18 Feb 2021, 20:03

Re: How to remove an onmessage?

09 Jun 2022, 19:06

boiler wrote:
09 Jun 2022, 18:36
You could keep all the function names in an array, and call a function that clears all of them then binds any it finds in the array. The destruction would then remove the appropriate array item then call the function that removes all then rebinds what remains in the array.
You could provide an working example?
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: How to remove an onmessage?

09 Jun 2022, 19:49

Actually, it's better just to turn off the one you want rather than turning them all off and then one back on. And since function objects are required to have more than one for the same message, it would work like this:

Code: Select all

CoordMode, ToolTip, Screen

Gui, Show, w200 h200 ; to receive the messages
funcs := [Func("func1"), Func("func2")] ; define all functions
for each, func in funcs
	OnMessage(0x200, func)

MsgBox, Both funcs are active

OnMessage(0x200, funcs.1, 0) ; turn off the first function

MsgBox, Only func2 is active
return


func1() {
	ToolTip, % "func1: " A_TickCount, 0, 0, 1
}

func2() {
	ToolTip, % "func2: " A_TickCount, 0, 40, 2
}

I'll leave it to you implement it for your class destructor.
kauan014
Posts: 55
Joined: 18 Feb 2021, 20:03

Re: How to remove an onmessage?

11 Jun 2022, 15:02

@lexikos do you think calling onmessage like this could impact somehow the performance of the script?
i mean, losing a message for example, or more use of CPU, idk ?

Code: Select all

OnMessage.Init()
Gui, Show, w600 h200
Return

F1:: ; Register a new message
OnMessage.New(0x0201, "WM_LBUTTONDOWN")
Return

F2:: ; Remove the message
OnMessage.Remove(0x0201, "WM_LBUTTONDOWN")
Return

WM_LBUTTONDOWN(wParam, lParam, Msg, Hwnd) {
   FileAppend, WM_LBUTTONDOWN`n,*
}

Class OnMessage {

   Init() {
      this.New(0x0201, "OnMessage.WM_LBUTTONDOWN")
   }

   New(msg, function) {

      If (!this.msg)
         this.msg := {}

      If (!this.msg[msg])
         this.msg[msg] := {}

      this.msg[msg][function] := Func(function).Bind(this)

      For Index, Func in this.msg[msg]
         OnMessage(msg, Func)

   }

   Remove(msg, function) {
      func := this.msg[msg][function]
      OnMessage(msg, func, 0)
   }

   WM_LBUTTONDOWN(wParam, lParam, Msg, Hwnd) {
      FileAppend, ==> OnMessage.WM_LBUTTONDOWN`n,*
   }

}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, madensuyu1, peter_ahk and 348 guests