How to prevent the mouse from entering an application (or arbitrary area of screen)?
I have started work on a proof-of-concept application, which requires the following functionality.
- The app stays on top of all other apps - (no problem)
- If the mouse is focused on another program, and the left mouse button is down then the mouse must not be able to enter the application at all, and will stop at the edge, even if the user continues to attempt to drag it, and the focus must remain on the external application.
- If the user is not currently interacting with the external application, and enters my app, then they can do so and interact with the app as they choose.
The ideal solution would be to somehow prevent the mouse from ever entering a defined area of the screen (ie defined by the frame boundry). This restriction would be enabled only when the left-button is down.
Detecting whether the left-button is down outside my app is doable but tedious, using JNI.
Preventing the mouse ever entering a region or frame is still beyond me, after much searching.
The solution i have currently implemented involves the following:
1) polling and recording the mouse location state using MouseInfo every 10 ms, and so long as the mouse is not within the region (determined using Polygon.contains(mouseLocation))
2) using a MouseListener, and the mouseEntered and mouseExited events to record whether or not the mouse is in the frame. (The mouseEntered event will not be triggered if the mouse is being dragged on the external application)
3) if the polling determines that the mouse is inside the frame, but the mouseEntered event has not be fired, then the mouse-button IS thus down, and so i use the Robot to move the mouse back to the last recorded position outside the application.
This approach works somewhat, but is far from ideal, because the mouse is moved quickly then the last recorded position could be far from the edge of the app, and the point where the intrusion of the mouse into the app was detected could be deep within its borders.
It would be much better if i could exclude the region defined by the border of the app completely under the conditions i described. I would prefer not to use JNI if there is a pure java solution, but will do so if there is no other way.
Does anyone have a better solution to the one i implemented, whether with JNI or not?
Thank you in advance,
Re: How to prevent the mouse from entering an application (or arbitrary area of scree
I have thought of a possible new solution, requiring Hooks though:
- Intercepting all WM_MOUSEMOVE events, and passing them on to their intended targets only if the mouse pos is outside the app or the mouse button is up. That should prevent the mouse from entering my app when i don't want it to, as long as the interception happens fast enough. Not sure.
It will require more research for me to figure out how to do this, unless some kind developer can help with it or suggest a better solution. All advice will be appreciated.