Detect When Mouse Enters / Leaves Screen


(Joseph Sweeney) #1

I’m making a custom mouse cursor, and I want it to disappear (change back to OS cursor) when the mouse leaves the screen. I found a few values in the net.flashpunk.utils.Input class that give me mouse coordinates, and I found some in net.flashpunk.FP that give me absolute mouse position, but nothing to determine whether the cursor is over the game or not.

Any suggestions?


(Alex Larioza) #2

Using Mouse.Hide() and Mouse.Show() hides and shows the cursor respectively. Also, the mouse should reappear/revert to OS style when outside of the swf, regardless of what you do.


(Justin Wolf) #3

Yea, he might be using an older version of Flash Player. If I remember correctly, you had to determine when the mouse was outside of the SWF to make it reappear, else the Windows cursor would flicker. Maybe Adobe has fixed this with one of the newer versions.

But yea, running a simple test (local test and in Chrome’s Pepperflash) it seems that the Windows cursor does reappear when you move it outside the SWF area.


(Joseph Sweeney) #4

I guess I was unclear as to the nature of my problem. What I am trying to avoid is the custom cursor being “stuck” at the location the mouse left the screen. I can’t find a way to hide the custom cursor only when the cursor is not over the window.


(reopucino) #5

maybe you should make limit in your custom cursor…

if(Input.mouseX <=0 || Input.mouseX >=640 || Input.mouseY <= 0 || Input.mouseY >= 480)
{ 
    cursor.visible = false;
}
else cursor.visible = true;

(Joseph Sweeney) #6

The problem here is that when the mouse position is outside the screen, Input.mouseX and Input.mouseY return values for the last mouse position over the screen.

For example, is there any way to determine the screen’s absolute position? Then I could use Input.mouseFlashX and Input.mouseFlashY to determine whether or not it is in range.

EDIT: Just realized that Input.mouseX and Input.mouseFlashX return the same values for my uses… I guess I don’t understand the difference. Both only update the value when the cursor is over the screen. Does flash even have access to the OS’s cursor position?


(Jacob Albano) #7

If the cursor is outside of the SWF and it doesn’t have focus (you weren’t holding the mouse button when it left), Flash doesn’t know where the cursor is. It’s a limitation on the plugin for security reasons.


(Draknek) #8

Look at the Event.MOUSE_LEAVE event.


(Joseph Sweeney) #9

I’ve never used events in Flash before, so I’ll have to mess around until I get that to work. Seems like a plausible solution though.