- Home /
How can I trigger GetMouseButtonUp when the cursor leaves the game window? Or if the window is minimised, etc?
Hiya! I'm working on a game - not web based, will run fullscreen or in a window on the desktop - and I have what is essentially a click and drag functionality when you click and hold on the left mouse button. If the cursor leaves the window while dragging and then the mouse button is released out of the window, when the cursor is returned to the window the mouse still behaves as if the button is down i.e. still dragging while the mouse button is up.
To prevent this, is there a way to trigger the button release automatically when the cursor leaves the window? Or when the game window is minimised etc... Basically when the game loses focus, although I'm reluctant to say "loses focus" because I know that refers to something else to do with the GUI.
I feel like this must have been answered somewhere before, but I just haven't found the right keyword yet. I know how to write the code for the mouse part, if someone could point me in the direction of some code that lets you know when the cursor isn't on the window that would be amazing.
Thanks very much!
Romano
Have you looked into $$anonymous$$athf.Clamp() at all? It may be just what you need to clamp the mouse click inside the screen range.
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html
Thanks for that :) I can't figure out how that would solve the problem exactly though? I'll edit the question title - it might be a wee bit misleading.
Actually, I'll edit the whole thing, it could be much clearer...
Answer by Sisso · Apr 25, 2014 at 09:11 PM
I don't have sure, but I think that convert mouse position to viewport point and check if it out of bounds (< 0 or > 1)
var p = Camera.main.ScreenToViewportPoint(Input.mousePosition);
if (p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1) Debug.Log("Is OUT");
That's very interesting and might totally have answered my question if I wasn't such a dick and changed my question slightly while you were answering. Thanks very much though that could be the solution to at least part of my problem :)
I have a little knowledge in desktop, but I think that you will need to track what events do you receive and try to solve it by yourself. Add some logs into OnApplication* events and Update to find a pattern that you can use.
If I undestand, you said correctly that game lose focus.
I saw that in the documentation, but it does a pretty terrible job of explaining what its actually for. Do you know how OnApplicationFocus works?
Sorry, no. I work most in mobile.
Add some debug and test many different actions with your app to see. I think that click with mouse outside of the window, alt tab or any other app window that come in front (grab the focus) should invoke this event.
$$anonymous$$ouse outside the window without click, I think that the game will continue to play and Update will called. In this case, could be verify with ScreenToViewportPoint.
Answer by Romano · Apr 25, 2014 at 09:54 PM
Here's my current solution, which is actually working pretty decently for the window being minimised, but doesn't trigger anything when the mouse leaves the window yet.
Basically I have a boolean called
leftClickAndHoldActive
which is set to true when
Input.GetMouseButton(0) // Left mouse button held down.
is true.
Then at the end of my code that detects different kinds of clicks (Single, double, leftclick and hold etc) I have this:
else if (leftClickAndHoldActive == true && !(Input.GetMouseButton(0)))
{
print("Lost Focus");
}
Hi, i have the same problem when i click on a button and release outside it, do you find any other solution ? thanks