OnMessage - Multiple Functions registered for a single MsgNumber! Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 19:54

x() and y() are registered for 0x200 MsgNumber! when the mouse is moved inside gui window client area only Y() count works, but x() count does not work! Any workaround?
test.gif
test.gif (152.19 KiB) Viewed 2754 times

Code: Select all

onmessage(0x200, "x")
onmessage(0x200, "y")


global x = 20, y = 20

gui, add, text, w200 h200,

gui, show

return


guiclose:	;_____________ gui close ____________
exitapp


x()	;_____________
{
X++
tooltip, % "x" x " - y" y  
}

y()	;______________
{
y++
tooltip, % "x" x " - y" y  
}
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 19:59

You could combined them.
Please excuse my spelling I am dyslexic.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 20:03

Capn Odin wrote:You could combined them.
Yha, that is what I'm asking, how to "combine" onmessage(0x200, "x") and onmessage(0x200, "y")
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 20:24

I mean

Code: Select all

onmessage(0x200, "coord")
Coord() {
X()
Y()
}
or

Code: Select all

onmessage(0x200, "coord")
Coord() {
X++
Y++
ToolTip, % X ", " y
}
Edit: from the docs it seems that the problem may be that they are called too fast for the functions to terminate so the X() will never resolve since only one thread per registered function is allowed by default new X() calls won't happen. Try adding critical at the top of both X() and Y() in your original script.
Edit2: to clarify, I am saying that at the first message x() is called followed by y() but y is called before X finish, so x waits until y have finished but this never happens as both are called too often.
Please excuse my spelling I am dyslexic.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 21:16

You made it more complicated than it needs to be, User. The following is all that you need:

Code: Select all

gui, add, text, w200 h200
gui, show

OnMessage(0x200, "ToolTip")

ToolTip()
{
    MouseGetPos, MouseX, MouseY
    ToolTip, x%MouseX% y%MouseY%, %MouseX% + 20, %MouseY% + 20
}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 21:25

Osprey wrote:.
MouseX and MouseY? I don't remember mentioning anything related with mouse in this thread!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 21:31

User wrote:MouseX and MouseY? I don't remember mentioning anything related with mouse in this thread!
What are you using to move that mouse pointer in the GIF that you posted, then... your mind?
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 21:39

Osprey wrote:
User wrote:MouseX and MouseY? I don't remember mentioning anything related with mouse in this thread!
What are you using to move that mouse cursor in the GIF that you posted, then... your mind?
That's not of your business! I'm not in the mood to tell you how I move or don't move my mouse cursor!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 21:45

I see. Well, you might find using a mouse to be more comfortable and seeing your x coordinate update as you move might put you in a better mood.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 21:55

- Capn Odin gave what appears to be a working script for this, it seems that every time you move the mouse, you want both numbers to increase by 1, both numbers should be identical, but then the use of 'x' and 'y' would have no meaning.
- If you wanted to increase x each time a horizontal movement was made, and to increase y each time a vertical movement was made, you might want to record the mouse position each time, and do a numerical comparison. This could be accomplished using OnMessage with one custom function.
- [EDIT:] Unless you simply want to use OnMessage with 2 different functions for the sake of it, or for some other reason, and the x/y thing is just for demonstration purposes.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 22:02

jeeswg wrote: Unless you simply want to use OnMessage with 2 different functions for the sake of it, or for some other reason, and the x/y thing is just for demonstration purposes.
BINGO @jeeswg! Little kids nowadays like @Osprey are to slow to catch up simple things! (Little kid @Osprey don't be mad at me, ok?)
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 22:04

jeeswg wrote:- Capn Odin gave what appears to be a working script for this, it seems that every time you move the mouse, you want both numbers to increase by 1, both numbers should be identical, but then the use of 'x' and 'y' would have no meaning.
Capn Odin's two sets of code didn't work for me. Maybe I missed something.
jeeswg wrote:- If you wanted to increase x each time a horizontal movement was made, and to increase y each time a vertical movement was made, you might want to record the mouse position each time...
He's not using a mouse. He's using his mind. Have you written a function yet to read people's minds? That might actually come in really handy around here, come to think of it.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 22:07

- I think lexikos said something about chaining functions once, I'm not exactly sure, and I'm not sure where to find it. I was hoping someone else might post, if they knew.
- I don't know if the idea was like this or whether it was something quite different:

Code: Select all

OnMessage(0x200, "MyFunc1")
MyFunc1()
{
	;do stuff
	MyFunc2()
}
MyFunc2()
{
	;do stuff
}
[EDIT:] I found the quote:
jeeswg's 3-item wish list (GUI class name+GUI no icon, A_ThisHwnd/A_ThisIfWinCriteria on hotkey launch, InputBox HFONT) - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 55#p134155
Perhaps you are unaware that you can register multiple monitors for a given message.
Last edited by jeeswg on 06 Dec 2017, 22:13, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: OnMessage - Multiple Functions registered for a single MsgNumber!  Topic is solved

06 Dec 2017, 22:16

- It looks like it's relatively simple to use OnMessage with multiple functions for the same message.
OnMessage
https://autohotkey.com/docs/commands/OnMessage.htm
By default, when multiple functions are registered for a single MsgNumber, they are called in the order that they were registered.
- Maybe you need to do this:
global x := 20, y := 20
- Btw, @User, if you can find a good reason to post at this thread, please post there:
base.method() and this.base.method() Confusion - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40384
- [EDIT:] To use multiple functions per message, you may need to use function objects:
OnMessage
https://autohotkey.com/docs/commands/On ... e_v_Object
For backward compatibility, at most one function can be registered by name to monitor each unique MsgNumber -- this is referred to as the "legacy" monitor.
- [EDIT:] Try this at the start of your script:

Code: Select all

fnx := Func("x")
fny := Func("y")
onmessage(0x200, fnx)
onmessage(0x200, fny)
I was getting numbers for x and y that were similar but that were not quite the same, perhaps the script can't handle so many messages in such a short space of time.
- Anyhow, this is good, I had been curious about how to specify multiple functions for the same message, I was hoping someone would post how to do it, but now I've figured it out for myself.
Last edited by jeeswg on 06 Dec 2017, 22:48, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 22:46

jeeswg wrote:.
@jeeswg, here is the thing,

I like to put OnMessage() functions inside other functions and run them at script execution through static variables!

So, it means that, it is not necessary to use OnMessage() function at the beginning of the script, because OnMessage() is already inside the other functions!

But, as you can see in the example below, it seems that the OnMessage() from "y()" function deactivates the OnMessage() from "x()" function, and so, only y count occurs!

Code: Select all

global x := 20, y := 20

gui, add, text, w200 h200 vtext,

gui, show

return


guiclose:	;_____________ gui close ____________
exitapp


x()	;_____________
{
Static RunAtScriptExecution := onmessage(0x200, "x")
X++
tooltip, % "x" x " - y" y
}

y()	;______________
{
Static RunAtScriptExecution := onmessage(0x200, "y")
y++
tooltip, % "x" x " - y" y
}


So, @CapnOdin solution may be the best way to go! (Though I don't like to use OnMessage at the beginning of the script, but anyway, I can live with that!)

Code: Select all

OnMessage(0x200, "MouseMove")
MouseMove()
{
x()
y()
}

global x := 20, y := 20

gui, add, text, w200 h200 vtext,

gui, show

return


guiclose:	;_____________ gui close ____________
exitapp


x()	;_____________
{
X++
tooltip, % "x" x " - y" y
}

y()	;______________
{
y++
tooltip, % "x" x " - y" y
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 22:52

For this particular example, I would do this:

Code: Select all

OnMessage(0x200, "MouseMove")
MouseMove()
{
	static x := 20, y := 20
	x += 1, y += 1
	ToolTip, % "x" x " - y" y
}
Although I have demonstrated a way to trigger multiple functions per message above, it wouldn't be very reliable for monitoring WM_MOUSEMOVE (0x200). In my example code above using fnx and fny, x and y would have different values, even though, in theory, they should have the same value.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 23:07

jeeswg wrote:.
Wow, I would kiss you if I can jeeswg, onmessage(0x200, func("x")) and onmessage(0x200, func("y")) worked!

what function is "func()"? msgbox, func("x") " --- " func("y") seems to return nothing!

by the way, using "Critical" assures that x and y count are always the same!

Code: Select all

global x := 20, y := 20

gui, add, text, w200 h200 vtext vEditControl,

gui, show

return


guiclose:	;_____________ gui close ____________
exitapp


x()	;_____________
{
Static RunAtScriptExecution := onmessage(0x200, func("x"))

Critical
X++
guicontrol, , EditControl, % "x" x " - y" y
}

y()	;______________
{
Static RunAtScriptExecution := onmessage(0x200, func("y"))

Critical
y++
guicontrol, , EditControl, % "x" x " - y" y
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

06 Dec 2017, 23:15

It doesn't return text, that's because it's an object.

Code: Select all

q::
fn := Func("StrLen")
MsgBox, % IsObject(fn)
MsgBox, % Type(fn)
return

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

Type(Value)
{
    local m, f, e
    if IsObject(Value)
    {
        static nMatchObj  := NumGet(&(m, RegExMatch("", "O)", m)))
        static nBoundFunc := NumGet(&(f := Func("Func").Bind()))
        static nFileObj   := NumGet(&(f := FileOpen("*", "w")))
        static nEnumObj   := NumGet(&(e := ObjNewEnum({})))

        return ObjGetCapacity(Value) != ""  ? "Object"
             : IsFunc(Value)                ? "Func"
             : ComObjType(Value) != ""      ? "ComObject"
             : NumGet(&Value) == nBoundFunc ? "BoundFunc"
             : NumGet(&Value) == nMatchObj  ? "RegExMatchObject"
             : NumGet(&Value) == nFileObj   ? "FileObject"
             : NumGet(&Value) == nEnumObj   ? "Object::Enumerator"
             :                                "Property"
    }
    else if (ObjGetCapacity([Value], 1) != "")
        return "String"
    else
        return InStr(Value, ".") ? "Float" : "Integer"
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

07 Dec 2017, 00:03

jeeswg wrote:.
Thank you!

here is another example, fn := Func("StrLen"), the function "StrLen()" info is stored in "fn" variable as object! (or sort of)

https://autohotkey.com/docs/objects/Func.htm

Code: Select all

; Retrieve a reference to the function named "StrLen".
fn := Func("StrLen")

; Display information about the function.
MsgBox % fn.Name "() is " (fn.IsBuiltIn ? "built-in." : "user-defined.")
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: OnMessage - Multiple Functions registered for a single MsgNumber!

07 Dec 2017, 01:18

I feel so bad having missed this in the docs I decided to fabricate the reason it wasn't working. Like simulated threads ever is a problem.
Please excuse my spelling I am dyslexic.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Marium0505, william_ahk and 350 guests