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,