OnMessage(WM_MouseMove) to work only if gui window is active?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
alesyt0h
Posts: 214
Joined: 28 Jan 2015, 20:37

OnMessage(WM_MouseMove) to work only if gui window is active?

07 May 2021, 15:13

Hello all

I'm using this example on my gui script: https://www.autohotkey.com/docs/commands/Gui.htm#ExToolTip

My problem is the OnMessage function is being executed on every mouse movement, even if the gui window is not active, minimized or even destroyed. Yes I know this is how is supposed to work, and that I can disable that on message with "" parameter on it.

But I'm trying to make it work only when the gui window is active, and not execute it on everymouse move. I tried with If WinActive("ahk class AutoHotkeyGUI") inside the onmessage function and the tooltip is not showing if the window is not active but still it checks if the window is active or not on every mouse movement. I'd rather a better and clean solution for this.

This is driving me mad and cannot find anything about it.

How can I accomplish this?


Thank you all for your time reading my post :beer:
Regards
alesyt0h
Posts: 214
Joined: 28 Jan 2015, 20:37

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

07 May 2021, 19:24

mikeyww wrote:
07 May 2021, 16:42
You can unregister the function following WinWaitNotActive.
it will be the same as I said with if winactive {

I need to work it when the window is active, and idle when is minimized or non existent.
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

07 May 2021, 19:56

What are you trying to achieve? Do you have an example of a script that is not achieving that?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

07 May 2021, 20:46

alesyt0h wrote:
07 May 2021, 19:24
mikeyww wrote:
07 May 2021, 16:42
You can unregister the function following WinWaitNotActive.
it will be the same as I said with if winactive {
No, it would not be the same. It would not be constantly going to your function or constantly doing anything else either. You could use both WinWaitActive and WinWaitNotActive inside a loop, and when it reaches each of them, it would enable or disable the OnMessage respectively, then it wouldn't be going to your function triggered by OnMessage when it's not active, which is what you said you wanted. And if you're concerned about it constantly looping, it wouldn't be doing that. It would just stop and wait on either command that waits for the window to be active or not. And if your script is GUI event driven, then you can let that loop do its thing and the script will still respond to button presses or whatever, as is demonstrated in the script below. It beeps whenever the WM_MOUSEMOVE() function is called, and as you'll see, it is not called at any time when some other program's window is active even if you move the mouse over the GUI.

Code: Select all

Gui, +HwndGuiID
Gui, Add, Button, y30 w150 gButtonClicked, My button
Gui, Show, h80
OnMessage(0x0200, "WM_MOUSEMOVE")
loop {
	WinWaitNotActive, ahk_id %GuiID%
	OnMessage(0x0200, "WM_MOUSEMOVE", 0)
	WinWaitActive, ahk_id %GuiID%
	OnMessage(0x0200, "WM_MOUSEMOVE", 1)
}
return

ButtonClicked:
	MsgBox, Button was clicked
return

WM_MOUSEMOVE() {
	SoundBeep
}

GuiClose:
ExitApp
alesyt0h
Posts: 214
Joined: 28 Jan 2015, 20:37

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 02:15

Thank you both!

as the boiler post, that is best solution for what I want, inside the loop the winwait will be waiting for that window and not executing any function call to check if the window is active or not and it will not cause an useless cpu usage which that was the main reason.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 06:18

Without a cycle:

Code: Select all

OnMessage(0x6, "WM_ACTIVATE")
Gui, Show, w300 h300

WM_ACTIVATE(wp, lp) {
   OnMessage(0x200, wp ? "WM_MOUSEMOVE" : "")
}

WM_MOUSEMOVE(wp, lp) {
   ToolTip WM_MOUSEMOVE
}
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 06:26

That's definitely the way to go. I tried that, and I got some weird thing happening where the title bar of the GUI would not be shaded correctly to indicate when it was active or not (even when I activated another window, both title bars would show that they were the active window). I must have done something else to cause that, but I can't see what.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 06:38

I don't see it when I run yours. Mine must have done something different, although I used 0x6 WM_ACTIVATE and there weren't that many moving parts. Instead of a ternary operator, I used an if statement, but it otherwise seems like it would be the same. Not important anyway. Just wondering what I may have done to cause that.
alesyt0h
Posts: 214
Joined: 28 Jan 2015, 20:37

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 06:40

teadrinker wrote:
08 May 2021, 06:18
Without a cycle:

Code: Select all

OnMessage(0x6, "WM_ACTIVATE")
Gui, Show, w300 h300

WM_ACTIVATE(wp, lp) {
   OnMessage(0x200, wp ? "WM_MOUSEMOVE" : "")
}

WM_MOUSEMOVE(wp, lp) {
   ToolTip WM_MOUSEMOVE
}
I’m not understanding the wp and lp variables, what are they for? And how is wp will be true of I want it only on the gui window?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 07:01

Yeah, mine was something like:

Code: Select all

WM_ACTIVATE(wparam) {
	if wparam
		OnMessage(0x200, "WM_MOUSEMOVE", 1)
	else
		OnMessage(0x200, "WM_MOUSEMOVE", 0)
}
..which could also be:

Code: Select all

WM_ACTIVATE(wparam) {
   OnMessage(0x200, "WM_MOUSEMOVE", !!wparam)
}
When I plug that into your code, I don't see the problem. Strange. Maybe I had a typo or something. I didn't save it as I did it in CodeQuickTester.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 07:08

boiler wrote: OnMessage(0x200, "WM_MOUSEMOVE", 0)
Must be OnMessage(0x200, ""), 0 is for the function object.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: OnMessage(WM_MouseMove) to work only if gui window is active?

08 May 2021, 07:12

Thanks. Doesn’t seem to cause a problem, though.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, mapcarter, robnicholson, Rohwedder and 342 guests