How do I detect if the mouse has left the window when the game isn't fullscreen?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MadTinkerer

I’m trying to implement scrolling the screen when you move the mouse to the edge of the screen, like in a Blizzard RTS. When the game is fullscreen, there’s no problem. But when the game is windowed and I move the mouse off of the window, get_tree().get_root().get_mouse_position() seems to think that the mouse is still on the window at the last position that it actually was. For example: the mouse’s x won’t be less than zero even if I move it left off of the window.

The DisplayServer class has what looks like some relevant values in the WindowEvent enum, but it looks like what I want to actually do (just detect if the mouse has left and/or returned to the window) is either unimplemented or undocumented. Or I’m missing something obvious.

:bust_in_silhouette: Reply From: sry295

it in notification

func _notification(what):
    if what == NOTIFICATION_WM_MOUSE_ENTER:
	    print("enter")
    elif what == NOTIFICATION_WM_MOUSE_EXIT:
	    print("exit")

you could also try

if what == NOTIFICATION_WM_FOCUS_IN:
	print("focus in")
elif what == NOTIFICATION_WM_FOCUS_OUT:
	print("focus out")

to detect is user tab to other window

Perfect! Exactly what I was looking for!

And now I have some idea of how to use the _notification() function!

MadTinkerer | 2022-04-07 04:48