How to detect mouse out of window?

BlitzMax Forums/BlitzMax Programming/How to detect mouse out of window?

anawiki(Posted 2007) [#1]
Hi
You can play Runes of Avalon in windowed mode. One of the portals complains that when you move the mouse out of the window, custom cursor is still in the window (you need to move it very fast).

How I can detect that mouse cursor is out of window in graphics windowed mode?

best
Roman


Dreamora(Posted 2007) [#2]
get the mouse position from the OS via WinAPI or something else and calculate if it is within the window rect.


Grey Alien(Posted 2007) [#3]
buy my framework ;-) (actually no Joke, it's probably very useful for someone like you, there's tons of stuff like this...)

Anyway you need this in WINDOWS ONLY:

?win32
Extern "win32"
	Function GetCursorPos%(point: Byte Ptr)
	Function GetWindowInfo(hWnd%, WindowInfo: Byte Ptr)       
End Extern
?

Type TPoint
	Field X
	Field Y
	
	Method Set(tx,ty)
		X = tx
		Y = ty
	End Method
End Type

Type TWindowInfo
  	Field cbSize
	Field wl,wt,wr,wb 'need to inline the TRect as 4xIntegers
	Field cl,ct,cr,cb 'need to inline the TRect as 4xIntegers
    Field dwStyle
    Field dwExStyle
    Field dwWindowStatus 
    Field cxWindowBorders
    Field cyWindowBorders
    Field atomWindowType:Short 'Research shows an Atom to be a 16-bit value (I hope)
	Field wCreatorVersion:Short
End Type

' -----------------------------------------------------------------------------
' ccGetCursorPos: Windows API call to get the mouse cursor position relative to the desktop top left
' -----------------------------------------------------------------------------
Function ccGetCursorPos:TPoint()
	'Returns a TPoint
	Local point:TPoint = New TPoint
	?Win32 
	If Not GetCursorPos(point) Then
		'Don't error any more otherwise you get a bomb if the screensaver kicks in
		'when "on resume, password protect" is ticked.
'    	ccRunTimeError ("Error Getting Cursor Pos")	
		Return Null
	Else
		Return point
	EndIf
	?
	Return point 'safety for non-windows
End Function

	Method SetClientCoords()
		?Win32
		Local wi:TWindowInfo = ccGetWindowInfoHandle(WindowHandle)
		ClientX = wi.cl
		ClientY = wi.ct
		?
	End Method

' -----------------------------------------------------------------------------
' ccGetWindowInfoHandle: Uses Windows API call to return useful info such as any window area and client area
' -----------------------------------------------------------------------------
Function ccGetWindowInfoHandle:TWindowInfo(hWnd%)
	'Pass in a handle to a window.
	Local wi:TWindowInfo = New TWindowInfo
	?Win32
	wi.cbSize = SizeOf(wi)
	GetWindowInfo(hwnd,wi)	
	?
	Return wi 'leave outside ?Win32 bit for safety
End Function       




There I think that's everything, if something seems missing post again.

Haven't got time to show you how to use it, but it's obvious really. e.g. get the client window X/Y relative to desktop, then get the mouse coords relative to desktop, then check to see if mouse is outside client window (you should know it's width and height of course), then you can do some jiggery pokery to hide the mouse or SLIDE it in an out of the window in a lovely manner like my games do ;-)

When you get it working on a MAC via MacOSX calls please let me know! :-)

[edit] You can get a window handle like this: Local hWnd% = GetActiveWindow(). I would advise doing this STRAIGHT AFTER creating your windowed mode and storing it globally. Oh GetActiveWindow is another win32 API call - put this in the extern section: Function GetActiveWindow%()


degac(Posted 2007) [#4]
Graphics 640,480,0
Local here:String=""

While Not AppTerminate()
	PollEvent()
	Cls
	DrawText here,100,100
	Select CurrentEvent.ID
	
		Case EVENT_APPSUSPEND,EVENT_MOUSELEAVE

		here="MouseLeave"
		Case EVENT_MOUSEENTER
		here="MOuseEnter"
		Case EVENT_APPRESUME
		Case EVENT_APPTERMINATE

	End Select
	
	Flip
	
Wend

It seems to work for me


degac(Posted 2007) [#5]
Sorry double post


anawiki(Posted 2007) [#6]
@degac: Thanks. It works fine :)

@Grey: I am sure your framework is cool... I would buy if I was about to start my indie adventure, but now we have our own nasty framework.

best
Roman


Grey Alien(Posted 2007) [#7]
pah you chose the easy code, just "hide the cursor" instead of neatly slide it in and out, well my games will poo poo on yours just because of this single feature ;-p