Java xeyes - Follow the mouse cursor location outside a JFrame

A Java xeyes solution - I started working on my Java speech recognition app again today, and in the process I saw some source code I thought I should post here. When I was developing this app, I thought it would fun to put a GUI on it, and when I thought about what sort of GUI it should have, I thought of the old X-Windows xeyes app. I looked around to see if anyone had written a "Java xeyes" application, but from what I've seen, nobody has.

A big problem with trying to create a Java xeyes app is the need to follow the mouse cursor around the screen while (a) your Java application isn't the foreground app and (b) the mouse cursor location is not over your JFrame.

As I tried to tackle this problem with the usual Java MouseListener or MouseMotionListener I thought the problem couldn't be solved with JNI alone, but then I found the Java MouseInfo class, and the problem was (kinda) solved.

Java xeyes - Following the mouse outside a JFrame

Here's some Java source code that demonstrates a potential solution:

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;

/**
 * PROGRAM: A Java xeyes starter
 * PURPOSE: Show how to get the mouse cursor location (position) 
 *          outside a Java JFrame.
 * DATE:    March 27, 2011
 * VERSION: 0.1
 * COPYRIGHT:
 * Copyright 2011, alvin j. alexander, http://devdaily.com
 * This work is licensed under a Creative Commons 
 * Attribution-ShareAlike 3.0 Unported License;
 * see http://creativecommons.org/licenses/by-sa/3.0/ 
 * for more information.
 *
 */
public class JavaXeyes
{
  public static void main(String[] args)
  {
    while (true)
    {
      PointerInfo info = MouseInfo.getPointerInfo();
      Point p = info.getLocation();
      System.out.format("LOC %d, %d\n", p.x, p.y);
      sleep();
    }
  }
  
  private static void sleep()
  {
    try
    {
      Thread.sleep(500);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
  }
}


Java xeyes - The original X-Windows xeyes GUI
image courtesy of
wikipedia

If you're an experienced Java developer you can see that the problem with this approach is that you're constantly polling the system, asking it where the mouse is currently located, instead of listening to mouse events. Sadly, as mentioned, the MouseListener and MouseMotionListener classes only work when the mouse cursor is positioned over your JFrame, so as far as I know, this is the only way to get the mouse location (position) in a Java application without resorting to JNI.

Java xeyes - Summary

I'll keep digging around for other ways to solve this Java xeyes problem, but until then, this is the only non-JNI solution I know. If you know of another approach that works the way xeyes needs to work, please post a comment below, I'd love to hear about other options.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.