Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4009555
Votes 58
Synopsis RFE: API to location of mouse on desktop
Category java:classes_awt
Reported Against 1.1 , 1.4 , 1.1.4 , 1.3.1 , merlin-beta2
Release Fixed 1.5(tiger)
State 10-Fix Delivered, request for enhancement
Priority: 4-Low
Related Bugs 4706304 , 4717900 , 4908781
Submit Date 16-OCT-1996
Description
Applix has requested an API to return the location of the mouse on the desktop.  They would use this to place windows at the mouse location.

=============================================================
Another report:

I would like to obtain the current mouse location without waiting for any events.

I'm looking for something simple, like:

public class java.awt.Mouse
{
  /**
   * @return A Point representing the mouse's current X and Y coordinates.
   */
  public static native Point getCurrentPosition();
}

An example of why this is useful is:
1/ Load up Netscape
2/ Go to a really long page that has lots of URLs
3/ Click anywhere on the page that isn't over an URL
4/ Let go of the mouse; don't ever touch it
5/ Use the cursor keys (Up Arrow, Down Arrow, etc.) to scroll the long 
   page up and down
6/ Watch the mouse cursor change as an URL scrolls behind the mouse cursor
   (again, without so much as breathing on the mouse)

In order for Netscape to change the shape of the mouse cursor, it needs 
to know if the mouse's current location on the screen is over an URL or 
not.  To do this, whenever you scroll the page with the cursor keys 
(which are far from being a Mouse Event), Netscape must query to see 
where the mouse is currently situated on the screen.  If that location is 
on top of an URL, it changes the mouse shape to a Hand Pointer, or some 
such thing.

 xxxxx@xxxxx  1997-11-12
=============================================================
Work Around
N/A
Evaluation
 xxxxx@xxxxx  1997-12-08:
This sounds like a very reasonable request to me. Changing state to Evaluated.

commit to tiger since Robot does not have provide this.

 xxxxx@xxxxx  2000-01-20

As per 4717900, we probably will also want to add a convenience method for checking if the mouse cursor is over a particular Component.
 xxxxx@xxxxx  2003-01-06




See suggested fix for new APIs developed for this RFE.
======================================================================
Comments
  
  Include a link with my name & email   

Submitted On 24-MAR-1998
sharmats
I'm also looking for a  solution to a similar problem. I want to get/set the
location of the cursor on the desktop(or relative to the component) In case you
have found a workaround,please let me know.
Thanks in anticipation.


Submitted On 06-AUG-1998
MarkCollette
On multi-headed displays, it is very important to be able to open a window,
dialog, etc. where the mouse is, as that is where the user is looking.
Users tend not to guess correctly which of the 3+ monitors to look at to find
the newly openned window, instead they assume the application has hung.
Being able to find out exactly where the mouse is, without having to latch onto
the EventQueue and intercept all messages, would be great.


Submitted On 06-MAR-2001
LoneJustice
I came across this bug a while back, whilst writing a game.
It has complicated controls so I want to use many keys on
the keyboard as essentially a load of mouse buttons. Only,
when you press them, it isn't then obvious where the mouse
is. My workaround is to always keep track of the mouse
position via mouseMoved and dragged, but it's not very nice.
Please fix this.
@see
http://forum.java.sun.com/read/16789542/qA3ZeSvMfHa8AAjJ_#LR
@see
http://forum.java.sun.com/read/16800583/qAsHvDPRHD3oAAaJC#LR


Submitted On 01-JUL-2001
dafnaitzchak
I'm also looking for a solution , 
I want to open dialogs at the mouse location
without keeping treck by listening to the mouse event
Dafna


Submitted On 09-MAR-2002
minlu_us
Very important when the mouse operation goes beyond the
current Java application to the entire desktop. It will
be useful for many purposes.


Submitted On 18-APR-2002
kents1
I have come across the same problem.  I want to use the 
mouse to rotate a 3D scene.  The only way to do this is 
capture the mouse event generated, and use robot to set it 
back to the centre.  More control over the mouse would be 
great.


Submitted On 23-JUL-2002
pcdennison
What happened to this RFE? It seems to have been ignored, 
despite the simplicity of the request and the number of votes.


Submitted On 31-AUG-2002
moorejohn
I have a related requirement:  I would like to be able to
sample, or calculate, the direction and speed of the mouse,
irrespective of the location of cursor (i.e., even when the
cursor is pegged at an edge of the screen!).  Game-related,
as you might guess.


Submitted On 17-OCT-2002
msukhiashvili
Stil not fixed?!?!!?? 

:(((((


Submitted On 26-APR-2003
kzilli
Does someone know of a workaroung using JNI?
Please post!


Submitted On 23-JUL-2003
fsamuels
JNI Solution:
WINDOWS:
package com.liquid.win.util;

/**
 * This is the windows implementation of MousePosition.  It
makes use of a native .dll 
 * called MousePosition.dll for the implementation of
getMousePosition();
 * 
 * Creation date: (4/18/2002 10:13:48 AM)
 * @author: PHD
 */
public class MousePosition extends
com.liquid.util.MousePosition {

	/**
	 * The name of the DLL used to implement getting the mouse
position
	 */
	private final static java.lang.String LIBRARY =
"MousePosition"; //$NON-NLS-1$

	/**
	 * MousePosition constructor comment.
	 */
	public MousePosition() {
		super();
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (4/18/2002 10:14:21 AM)
	 * @return java.awt.Point
	 */
	public native java.awt.Point getMousePosition();
	
	
	/**
	 * Determines if this platform supports this implementation 
	 * of MousePosition.
	 * Creation date: (4/18/2002 10:32:40 AM)
	 * @return boolean
	 */
	public boolean isSupported() {
		return com.liquid.util.SysInfo.isWindows();
	}
	
	/**
	 * @see com.liquid.util.MousePosition#init()
	 */
	protected void init() throws Exception {
		System.loadLibrary(LIBRARY);
	}

}

// MousePosition.cpp : Defines the entry point for the DLL
application.
//

#include "stdafx.h"
#include "com_liquid_win_util_MousePosition.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

extern "C"
JNIEXPORT jobject JNICALL
Java_com_liquid_win_util_MousePosition_getMousePosition
  (JNIEnv *env, jobject obj)
{
	POINT pt;
	GetCursorPos(&pt);
	jint x = pt.x;
	jint y = pt.y;
	jclass ptClass;
	jmethodID constructorID;
	jobject result;
	ptClass = env->FindClass("java/awt/Point");
	if(ptClass == NULL) return NULL;
    constructorID = env->GetMethodID(ptClass,"<init>","(II)V");
	if(constructorID == NULL) return NULL;
	result = env->NewObject(ptClass,constructorID,x,y);
	if(result == NULL) return NULL;
	env->DeleteLocalRef(ptClass);
	return result;
}


Submitted On 23-JUL-2003
fsamuels
LINUX: (X11)
package com.liquid.linux.util;

import java.awt.Point;

import com.liquid.util.SysInfo;
/**
 * This is the Linux implementation of MousePosition.
 * 
 * Copyright (c) 2002 Liquid Communication Systems, LLC
 * Creation Date: Aug 24, 2002 1:10:55 PM
 * 
 * @author DFS
 * @version $Revision: 1.3 $
 */
public class MousePosition extends
com.liquid.util.MousePosition {

	/**
	 * The shared libarary to load for natively obtaining the
MousePosition.
	 */
	private final static java.lang.String LIBRARY =
"libMousePosition.so"; //$NON-NLS-1$

	/**
	 * The pointer retrieved natively that points to the open X
display
	 */
	private int displayPtr;

	/**
	 * Default constructor.
	 */
	public MousePosition() {
		super();
	}
	
	/**
	 * @see com.liquid.util.MousePosition#getMousePosition()
	 */
	public Point getMousePosition(){
		return this.getNativeMousePosition(this.displayPtr);
	}
	
	/**
	 * @see com.liquid.util.MousePosition#isSupported()
	 */
	public boolean isSupported() {
		return SysInfo.isLinux();
	}

	/**
	 * Initializes the native library that implements the
native retrieval of the current 
	 * mouse position.  This method assumes that the native
library has the filename 
	 * specified by LIBRARY and is located in the user's
current working directory.  This 
	 * simplifies the process of loading the library (as
opposed to using loadLibrary, which 
	 * appears more error prone).  This also opens the X
Windows Display for subsequent 
	 * getMousePostion() calls and adds a shutdown hook for
closing the display on VM
	 * exit.
	 * 
	 * @see com.liquid.util.MousePosition#init()
	 */
	protected void init() throws Exception {
	
System.load(SysInfo.getUserDir()+SysInfo.getFileSeparator()+LIBRARY);
		this.displayPtr = this.openDisplay();
		System.out.println("Opened display at address
"+this.getDisplayPtrString()+"."); //$NON-NLS-1$ //$NON-NLS-2$
	
Runtime.getRuntime().addShutdownHook(this.getDisplayCloseShutdownHook());
	}
	
	/**
	 * Creates and returns a nicely formatted hex string
representing the memory 
	 * address pointed to by the displayPtr.
	 * 
	 * @return String - formatted displayPtr address
	 */
	private String getDisplayPtrString() {
		return "0x"+Integer.toHexString(this.displayPtr);	
//$NON-NLS-1$
	}
	
	/**
	 * Creates and returns a Thread that is responsible for
closing the 
	 * display at VM shutdown to ensure everything is cleaned
up nicely.
	 * However, even if the VM is terminated, the X Display
that is open 
	 * should get released anyway.  This is just to ensure
we're being a 
	 * good citizen in the X Windows world.
	 * 
	 * @return Thread - the shutdown hook
	 */
	private Thread getDisplayCloseShutdownHook() {
		return new Thread("MousePosition Shutdown Hook For Closing
X Display") { //$NON-NLS-1$
			public void run() {
				System.out.println("Closed display at address
"+getDisplayPtrString()+"."); //$NON-NLS-1$ //$NON-NLS-2$
				closeDisplay(displayPtr);	
			}
		};		
	}
	
	/**
	 * The native method responsible for obtaining the current
mouse position.  
	 * 
	 * @param displayPointer int - the native pointer to the
open X Display
	 * @return java.awt.Point - the Point representing the
current Mouse Position
	 */
	private native java.awt.Point getNativeMousePosition(int
displayPointer);

	/**
	 * Opens an X Windows Display so the mouse position can be
later retrieved.  
	 * The int returned is the pointer to the open display.
	 * 
	 * @return int - the pointer to the open X display
	 */
	private native int openDisplay();

	/**
	 * Closes the open X Windows Display.
	 * 
	 * @param displayPointer int - the pointer to the open X
Display
	 */
	private native void closeDisplay(int displayPointer);
}


Submitted On 23-JUL-2003
fsamuels
#include <X11/Xlib.h>
#include <stdio.h>	/* For printf() */
#include <unistd.h>	/* For usleep() */
#include "com_liquid_linux_util_MousePosition.h"
//http://www.linuxgazette.com/issue78/tougher.html

/*
 * Class:     MousePosition
 * Method:    getNativeMousePosition
 * Signature: (I)Ljava/awt/Point;
 */
JNIEXPORT jobject JNICALL
Java_com_liquid_linux_util_MousePosition_getNativeMousePosition
  (JNIEnv * env, jobject jthis, jint displayPtr) {
  	Display *display = (Display*)displayPtr;
  	Window root_return, child_return;
	int root_x_return, root_y_return;
  	int win_x_return, win_y_return;
  	unsigned int mask_return;
	int SCREEN = 0;

  	//printf("Checking for Null display.");
  	if(!display) return NULL;

  	//printf("Getting root window.\n");
  	Window root_win = RootWindow(display,SCREEN);
	 
	//printf("Getting mouse position.");
	Bool xqueryResult = XQueryPointer(display, root_win,
&root_return, &child_return, 
			&root_x_return, &root_y_return, &win_x_return,
&win_y_return, &mask_return);
	
	jint x = root_x_return;
	jint y = root_y_return;
	jclass ptClass;
	jmethodID constructorID;
	jobject result;
	ptClass = env->FindClass("java/awt/Point");
	if(ptClass == NULL) return NULL;
    constructorID = env->GetMethodID(ptClass,"<init>","(II)V");
	if(constructorID == NULL) return NULL;
	result = env->NewObject(ptClass,constructorID,x,y);
	if(result == NULL) return NULL;
	env->DeleteLocalRef(ptClass);
	return result;
}

/*
 * Class:     MousePosition
 * Method:    openDisplay
 * Signature: ()I
 */
JNIEXPORT jint JNICALL
Java_com_liquid_linux_util_MousePosition_openDisplay
  (JNIEnv *env, jobject jthis) {
	//printf("Opening the display.\n");
	Display *display = XOpenDisplay(NULL);
	return (jint)display;

}

/*
 * Class:     MousePosition
 * Method:    closeDisplay
 * Signature: (I)V
 */
JNIEXPORT void JNICALL
Java_com_liquid_linux_util_MousePosition_closeDisplay
  (JNIEnv *env, jobject jthis, jint displayPtr){
	//printf("Closing the display.");
	XCloseDisplay((Display*)displayPtr);
}

Enjoy.. questions/comments to forrest AT liquidcs DOT com



PLEASE NOTE: JDK6 is formerly known as Project Mustang