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 3 of 3

Thread: How to get Mouse Position even if it is not within our application?

  1. #1
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default How to get Mouse Position even if it is not within our application?

    A common problem is wanting to retrieve the position of the mouse even if it is not within a Component of your application. This can be done using the following code, as an example. It Displays the mouse position every 100 thread tics.

    import java.awt.MouseInfo;
     
    public class Mouse {
    	public static void main(String[] args) throws InterruptedException{
    		while(true){
    			Thread.sleep(100);
    			System.out.println("("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
    		}
    	}
    }

  2. The Following 7 Users Say Thank You to Freaky Chris For This Useful Post:

    ChristopherLowe (January 4th, 2012), javapenguin (June 13th, 2011), JavaPF (January 4th, 2012), muralidhar (January 1st, 2010), pioneer (February 12th, 2011), snowguy13 (January 1st, 2012), Tjstretch (January 3rd, 2012)


  3. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to get Mouse Position

    Thanks. How would I change the coordinates to be relative to an arbitrary active window or tab ?

  4. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to get Mouse Position

    Is this so you can get Mouse Coords within you own Java application window space?

    If so consider this:
    http://docs.oracle.com/javase/6/docs...nListener.html

Similar Threads

  1. Replies: 8
    Last Post: April 21st, 2013, 08:20 AM
  2. How to create a system wide mouse or keyboard hook?
    By Freaky Chris in forum Java Native Interface
    Replies: 17
    Last Post: June 17th, 2009, 01:06 PM