How to detect that mouse cursor leaves script's gui?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Zvonko
Posts: 210
Joined: 19 Jun 2015, 11:52

How to detect that mouse cursor leaves script's gui?

14 Aug 2016, 07:15

What would be the simplest way to detect that the mouse cursor is moved outside the script's gui (without calculating coordinates etc.)?

Edited: I forgot to mention that in my script I already use OnMouseMove for other purpose, so I prefer a solution which works inside OnMouseMove.

Code: Select all

Gui Test: +HwndIdTest -dpiScale
Gui Test: Show, x50 y50 w800 h500

OnMessage(0x200, "OnMouseMove")
 
Return

OnMouseMove()
{
   Global IdTest
   MouseGetPos mx, my, IdWindow
   WinGetTitle, WinT, ahk_id %IdWindow%
   ToolTip The cursor is in window %WinT%, 150, 3
   If (IdWindow != IdTest)					; Doesn't work,
      MsgBox Out! Now in %WinT%!			; of course.
}
 
TestGuiClose:
ExitApp
 
Last edited by Zvonko on 14 Aug 2016, 10:58, edited 1 time in total.
For me as a newbie it is frustrating to fail due to "little" problems in projects in which the entire concept and even complex functions are working perfectly...
Gicu
Posts: 111
Joined: 19 Aug 2014, 08:19
Location: Italy

Re: How to detect that mouse cursor leaves script's gui?

14 Aug 2016, 09:44

with a timer seems to work

Code: Select all

Gui Test: +HwndIdTest -dpiScale
Gui Test: Show, x50 y50 w800 h500
 
SetTimer, onout
Return

onout:

   MouseGetPos mx, my, IdWindow
   WinGetTitle, WinT, ahk_id %IdWindow%
   ToolTip The cursor is in window %WinT%, 150, 3
   If (IdWindow <> IdTest)
      ToolTip Out! Now in %WinT%!
      
Return 
Return
Zvonko
Posts: 210
Joined: 19 Jun 2015, 11:52

Re: How to detect that mouse cursor leaves script's gui?

14 Aug 2016, 10:57

Thank you very much, Gicu.
I forgot to mention that in my script I already use OnMouseMove for other purpose, so I prefer a solution which works inside OnMouseMove.
For me as a newbie it is frustrating to fail due to "little" problems in projects in which the entire concept and even complex functions are working perfectly...
punchin
Posts: 439
Joined: 17 Jan 2014, 17:54

Re: How to detect that mouse cursor leaves script's gui?

14 Aug 2016, 11:13

You're already getting the mouse position, so just add in to OnMouseMove the calculation to test of its outside the gui. There is no simpler way than that that I know of. That really simple, especially if you use the built in variables A_GuiX, A_GuiY, A_GuiWidth, and A_GuiHeight.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to detect that mouse cursor leaves script's gui?

14 Aug 2016, 11:38

MSDN wrote:Posted to a window when the cursor moves. If the mouse is not captured, the message is posted to the window that contains the cursor. Otherwise, the message is posted to the window that has captured the mouse.

Source
In other words, if your Gui didn't capture the mouse and the cursor is outside your Gui you won't receive WM_MOUSEMOVE messages.
punchin
Posts: 439
Joined: 17 Jan 2014, 17:54

Re: How to detect that mouse cursor leaves script's gui?

14 Aug 2016, 11:53

If OnMouse move is not being called by the gui at times, you will need to set a timer to fire it manually.
Last edited by punchin on 16 Aug 2016, 12:44, edited 1 time in total.
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: How to detect that mouse cursor leaves script's gui?

14 Aug 2016, 13:12

You can use the parameters " OnMouseLeave " message to detect it.

Code: Select all

Gui Test: +HwndIdTest -dpiScale
Gui Test: Show, x50 y50 w800 h500

VarSetCapacity(tme,16,0)

NumPut(16,tme,0), NumPut(2,tme,4), NumPut(IdTest,tme,8)
OnMessage(0x200, "OnMouseMove")
OnMessage(0x2A3,"OnMouseLeave")

Return

OnMouseMove( wParam, lParam, Msg,hwnd ) {
global tme
DllCall( "TrackMouseEvent","uint",&tme )
    x := lParam & 0xFFFF
    y := lParam >> 16
tooltip Over window x=%x%  y=%y%
}

OnMouseLeave(){
tooltip leaving window
}

 
TestGuiClose:
ExitApp
Zvonko
Posts: 210
Joined: 19 Jun 2015, 11:52

Re: How to detect that mouse cursor leaves script's gui?

16 Aug 2016, 08:43

Thank you very much, Gicu, punchin, just me, noname!

Conclusion:
There really seems to be no simple AHK method (usable inside OnMouseMove) to detect that the mouse cursor leaves script's window! That's amazing, isn't it?


So I was forced to give up the OnMouseMove method and to use a timer...
For me as a newbie it is frustrating to fail due to "little" problems in projects in which the entire concept and even complex functions are working perfectly...
bbscrip
Posts: 1
Joined: 18 Sep 2017, 06:46

Re: How to detect that mouse cursor leaves script's gui?

20 Jun 2018, 12:23

This topic is a few years ago, but I did not find solution simple enough on same subject. I edited above example with small loop to track title and check for a change.

Code: Select all

Gui Test: +HwndIdTest -dpiScale
Gui Test: Show, x50 y50 w800 h500

OnMessage(0x200, "OnMouseMove")
 
Return

OnMouseMove()
{
   MouseGetPos mx, my, IdWindow
   WinGetTitle, WinT, ahk_id %IdWindow%
   Title_buffer := WinT
   ToolTip The cursor is in window %WinT%, 150, 3
   Loop, 
    {
      MouseGetPos mx, my, IdWindow  
	  WinGetTitle, WinT, ahk_id %IdWindow%
	 	  if !("" . Title_buffer = "" . WinT)
	     {
            MsgBox, The cursor is now out of %Title_buffer% GUI! 
			break
		 }
	  Sleep, 200
	}
}
 
TestGuiClose:
ExitApp
Works fine for me.
xan2622
Posts: 3
Joined: 02 Jan 2020, 16:06

Re: How to detect that mouse cursor leaves script's gui?

13 Oct 2023, 13:09

bbscrip wrote:
20 Jun 2018, 12:23
This topic is a few years ago, but I did not find solution simple enough on same subject. I edited above example with small loop to track title and check for a change.

Code: Select all

Gui Test: +HwndIdTest -dpiScale
Gui Test: Show, x50 y50 w800 h500

OnMessage(0x200, "OnMouseMove")
 
Return

OnMouseMove()
{
   MouseGetPos mx, my, IdWindow
   WinGetTitle, WinT, ahk_id %IdWindow%
   Title_buffer := WinT
   ToolTip The cursor is in window %WinT%, 150, 3
   Loop, 
    {
      MouseGetPos mx, my, IdWindow  
	  WinGetTitle, WinT, ahk_id %IdWindow%
	 	  if !("" . Title_buffer = "" . WinT)
	     {
            MsgBox, The cursor is now out of %Title_buffer% GUI! 
			break
		 }
	  Sleep, 200
	}
}
 
TestGuiClose:
ExitApp
Works fine for me.
Hi bbscrip. :salute:
Would you happen to have a AHK v2 version of this script ?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 230 guests