Position of cursor

901 views
Skip to first unread message

Frank

unread,
Jul 14, 2009, 9:41:13 AM7/14/09
to Google Web Toolkit
Hi,

I'm trying to get the cursor position on a page made with GWT. But I
can't find any function to do this

How can I get the x,y coordinates of my pointer. (I want to use this
with a popup window)

Regards,

Frank

Carver

unread,
Jul 14, 2009, 11:15:07 PM7/14/09
to Google Web Toolkit
Javascript will only give you a mouse position in response to an
event, AFAIK. Of course, events happen all the time (eg~ the mouse
moving). So you just need to decide which element in GWT is listening
for that event. See all the known objects that have the mouse move
handler here:
http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/event/dom/client/HasMouseMoveHandlers.html

GWT has a FocusPanel (ie~ a Panel that captures mouse and keyboard
events), but does not seem to have an equivalent FocusPopupPanel. So
you might want to do something like add a FocusWidget to your
PopupPanel.

~Carver

Slique.com - builds group memory by putting all your group's email,
files and documents in one place
I'm trying this out: http://five.sentenc.es/

Frank

unread,
Jul 15, 2009, 4:22:57 AM7/15/09
to Google Web Toolkit
With a simple javascript I can have the mouseposition anytime:

<!--
document.onmousemove = mouseMove;

function mouseMove(ev){
ev = ev || window.event;
var mousePos = mouseCoords(ev);

//document.Show.MouseX.value = mousePos.x;
//document.Show.MouseY.value = mousePos.y;
}

function mouseCoords(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
//->

How can I implement this in GWT and assign mousePos.x and mousePos.y
to class attributes?



On 15 jul, 05:15, Carver <jasoncar...@alum.mit.edu> wrote:
> Javascript will only give you a mouse position in response to an
> event, AFAIK.  Of course, events happen all the time (eg~ the mouse
> moving).  So you just need to decide which element in GWT is listening
> for that event.  See all the known objects that have the mouse move
> handler here:http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/g...

Carver

unread,
Jul 15, 2009, 4:45:58 PM7/15/09
to Google Web Toolkit
There is an issue posted for adding a keyboard listener to the
document here:
http://code.google.com/p/google-web-toolkit/issues/detail?id=1442

Based on the GWT team response to that issue, I would assume that
there is not going to mouse listener support for the entire document
anytime soon.

Whenever something doesn't have a good api yet in GWT, you can always
roll your own with JSNI. Something like this would be my approach:

public class MousePosition {
public static int x,y;
private static MouseMoveEvent mouseEvent; //from
com.google.gwt.event.dom.client.MouseMoveEvent

public static void setMouseEvent(MouseMoveEvent e){
mouseEvent = e;
x = mouseEvent.getX();
y = mouseEvent.getY();
sliqueJS.log.info("latest mouse event position (X,Y) = "+x
+","+y);
}
public static native void trackMousePos() /*-{
$wnd.document.onmousemove =
@com.slique.input.MousePosition::setMouseEvent(Lcom/google/gwt/event/
dom/client/MouseMoveEvent;);
}-*/;
}

Unfortunately, code is spewing an error I don't recognize. Can anyone
else tell what's going on here?

java.lang.IllegalArgumentException
at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:
103)
at com.google.gwt.dev.shell.ie.IDispatchImpl.callMethod
(IDispatchImpl.java:126)
at com.google.gwt.dev.shell.ie.MethodDispatch.invoke
(MethodDispatch.java:97)
at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke
(IDispatchImpl.java:294)
at com.google.gwt.dev.shell.ie.IDispatchImpl.method6
(IDispatchImpl.java:194)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6
(COMObject.java:117)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1925)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2966)
at com.google.gwt.dev.SwtHostedModeBase.processEvents
(SwtHostedModeBase.java:297)
at com.google.gwt.dev.HostedModeBase.pumpEventLoop
(HostedModeBase.java:565)
at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:411)
at com.google.gwt.dev.HostedMode.main(HostedMode.java:243)

~Carver
http://slique.com - builds group memory by putting all your group's
email, files and documents in one place

Carver

unread,
Jul 16, 2009, 12:48:13 PM7/16/09
to Google Web Toolkit
Here is a class for tracking mouse position. I haven't done the
testing, but I'd guess it has a significant performance penalty.
Every mouse movement triggers many extra operations, now. Hopefully
it at least gets you started, though!

//Copyright (c) 2009 Jason Carver, Slique
//MIT License
public static class MousePosition {
public static int x,y;

public static void setMouseLocation(int newx, int newy){
x = newx;
y = newy;
}
public static native void trackMousePos() /*-{
var updateMousePos =
@org.slique.client.sliqueJS.MousePosition::setMouseLocation(II);

var mouseCoords = function (ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + $wnd.document.body.scrollLeft -
$wnd.document.body.clientLeft,
y:ev.clientY + $wnd.document.body.scrollTop -
$wnd.document.body.clientTop
};
}

var mouseMove = function (ev){
ev = ev || $wnd.window.event;
var mousePos = $wnd.mouseCoords(ev);

updateMousePos(mousePos.x, mousePos.y);
};

$wnd.document.onmousemove = mouseMove;
}-*/;
}

Cheers,
Carver

http://slique.com - builds group memory by putting all your group's
email, files and documents in one place
I'm not being curt, I'm just using http://five.sentenc.es/
> ~Carverhttp://slique.com- builds group memory by putting all your group's

Dhinakar

unread,
Sep 8, 2015, 5:47:13 AM9/8/15
to GWT Users, Google-We...@googlegroups.com, frank...@gmail.com
Reply all
Reply to author
Forward
0 new messages