Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to prevent the mouse from entering an application (or arbitrary area of screen)?

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    Switzerland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to prevent the mouse from entering an application (or arbitrary area of screen)?

    Hello,

    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,
    Morgan Johnston


  2. #2
    Junior Member
    Join Date
    May 2012
    Location
    Switzerland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Entering and running a Program
    By Bijaysadhok in forum Java Theory & Questions
    Replies: 1
    Last Post: February 23rd, 2012, 07:15 AM
  2. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  3. Replies: 3
    Last Post: December 21st, 2011, 03:46 PM
  4. Not entering while loop
    By Kakashi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 02:15 PM
  5. Painting swing components to an arbitrary position?
    By ScummyChimp in forum AWT / Java Swing
    Replies: 1
    Last Post: September 2nd, 2009, 12:06 AM

Tags for this Thread