0

How to get the current location (x, y) of mouse cursor?

asked 2008-09-24 06:33:17 +0800

lendle gravatar image lendle
213 3

Hello,
I want to display a popup dialog next to current mouse cursor. The behavior of the
dialog is similar with context menu but with more sophisticated controls on it.
For example, if users click on "rename" on the context menu, I want to display a
dialog to inquire the new name right at current mouse position.

I've tried to use "onClick" event, but it seems provide relative x,y only.

Is there any solution to this?

Thanks !!

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2008-09-25 01:16:20 +0800

windperson gravatar image windperson
415 1

Hi lendle,
Maybe you need to write some javascript code for capture the mouse position, check this:
http://www.zkoss.org/doc/devguide/ch07s19.html

link publish delete flag offensive edit

answered 2008-09-25 01:44:56 +0800

robertlee gravatar image robertlee
561

updated 2008-09-25 01:45:41 +0800

Hi lendle,
From your requirement I don't see the need for getting x y position of the mouse,
You can achieve the same requirement by creating a new window component when your context menu (Menupopup) has been clicked. You can pass variables between different modal window by using set/getAttribute method.

If I am right and you need an example, please let me know by replying to this thread.

link publish delete flag offensive edit

answered 2008-09-25 04:01:52 +0800

lendle gravatar image lendle
213 3

Hello,
thanks for the suggestions

I finally achieve the effect I want by registering a handler for javascript "onmousemove" event.
The javascript handler has references to zk textbox components through uuid and thus
(x, y) information captured by the handler can be passed to zk environment.

(x, y) information is important to my application since I want to control the location of some popup
dialog for convenience.

Furthermore, it appears this mechanism will fail for modal dialogs, because modal dialogs are always at
the center of the client screen. Is there any way to overcome this limitation?

By the way, the (x, y) information may be very useful for some situation, is there any plan for such feature?

Thanks!

link publish delete flag offensive edit

answered 2008-09-25 14:53:01 +0800

dastultz gravatar image dastultz
797 9

lendle, I'm quite interested in your solution. If you would care to share the details of your implementation, I would appreciate it.

The current pop-up behavior operates on a delay after entering the element. I want the delay timer to start after the mouse stops moving. So I think I need the onmousemove handler as well as the x, y information.

Thanks.

/Daryl

link publish delete flag offensive edit

answered 2008-09-27 15:00:46 +0800

lendle gravatar image lendle
213 3

Hello, below is the code snippet
the javascript handler part is actually modified from some examples found on the web but I
forget the link to the source

<script type="text/JavaScript">
            var mouseXHolder=null;
            var mouseYHolder=null;
            
            function setHolder(idMouseXHolder, idMouseYHolder){
                mouseXHolder=document.getElementById(idMouseXHolder);
                mouseYHolder=document.getElementById(idMouseYHolder);
            }
            
            function requestFocus(idComponent){
                document.getElementById(idComponent).focus();
            }
            
            document.onmousemove=function(_e){
                if(mouseXHolder==null || mouseYHolder==null)
                    return;
                if(document.all){
                    mouseXHolder.value=event.clientX;
                    mouseYHolder.value=event.clientY;
                }else{
                    mouseXHolder.value=_e.clientX;
                    mouseYHolder.value=_e.clientY;
                }
                
                if (document.createEvent) {
                    var evt = document.createEvent('HTMLEvents');
                    evt.initEvent( 'blur', false, false);
                    mouseXHolder.dispatchEvent(evt);
                    evt = document.createEvent('HTMLEvents');
                    evt.initEvent( 'blur', false, false);
                    mouseYHolder.dispatchEvent(evt);
                    
                    var evt2 = document.createEvent('HTMLEvents');
                    evt2.initEvent( 'change', false, false);
                    mouseXHolder.dispatchEvent(evt2);
                    evt2 = document.createEvent('HTMLEvents');
                    evt2.initEvent( 'change', false, false);
                    mouseYHolder.dispatchEvent(evt2);
                } else if (document.createEventObject) {
                    mouseXHolder.fireEvent('onblur');
                    mouseXHolder.fireEvent('onchange');
                    mouseYHolder.fireEvent('onblur');
                    mouseYHolder.fireEvent('onchange');
                }

                return true;
            };
</script>

<zk:page style="height: 100%">
            <zk:zscript>      
                void onWindowCreate(){
                    Clients.evalJavaScript("setHolder('"+mouseX.getUuid()+"', '"+mouseY.getUuid()+"');");
                }    
            </zk:zscript>
            <zk:borderlayout>
                <zk:west style="width: 30%">
                    <zk:window id="treeWindow" onCreate="onWindowCreate();">
                        <zk:textbox id="mouseX" visible="true" value="0"/>
                        <zk:textbox id="mouseY" visible="true" value="0"/>
                    </zk:window>
                </zk:west>
                <zk:center>
                    <zk:textbox multiline="true" id="console" rows="50"/>
                </zk:center>
            </zk:borderlayout>
</zk:page>

link publish delete flag offensive edit

answered 2008-09-27 17:58:03 +0800

dastultz gravatar image dastultz
797 9

updated 2008-09-27 17:59:42 +0800

Hi lendle, thanks for the code. I took my own stab at it (also borrowing code from the web). I see you used clientX/Y. I think that's the position within the browser window rather than the page. IOW, it doesn't work when the page scrolls. Here is a fairly cross-browser solution I stole from

http://www.howtocreate.co.uk/tutorials/javascript/eventinfo

var mouseX;
	var mouseY;
	if (! evt) evt = window.event;
 	if (evt) {
		if (typeof(evt.pageX) == 'number') {
			mouseX = evt.pageX;
			mouseY = evt.pageY;
		} else if (typeof (evt.clientX) == 'number') {
 			mouseX = evt.clientX;
			mouseY = evt.clientY;
			if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
				mouseX += document.body.scrollLeft;
				mouseY += document.body.scrollTop;
			} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				mouseX += document.documentElement.scrollLeft;
				mouseY += document.documentElement.scrollTop;
			}
		}
	}

Also, I don't know if this might be useful, but sending an event to the server is easy with the comm object:

(JavaScript)

comm.sendUser(myComponentElement, 'onMyEvent', mouseX, mouseY);

(Java/server side)

myComponent.addEventListener("onUser", new EventListener() {
			public void onEvent(Event event) throws Exception {
				String[] args = null;
				if (event.getData() != null) {
					if (event.getData() instanceof String[]) {
						args = (String[]) event.getData();
					} else {
						args = new String[1];
						args[0] = event.getData().toString();
					}
				} 
				if (args != null) {
					String eventName = args[0];
					if (eventName.equalsIgnoreCase("onMyEvent")) {
						int mouseX = Integer.parseInt(args[1]);
						int mouseY = Integer.parseInt(args[2]);
						...
					}
				}
			}
		});

/Daryl

link publish delete flag offensive edit

answered 2008-10-03 06:15:40 +0800

lendle gravatar image lendle
213 3

Thanks, Dastultz

the information is really useful
I did not notice the difference between clientX/Y and pageX/Y
I should correct this in my code asap

thanks :)

link publish delete flag offensive edit

answered 2012-01-03 11:27:18 +0800

mayurk gravatar image mayurk
21

Hello,

I want to display popup at center to an image component which of size 70X320 (height X Width), is it possible to get the x,y position of an image....
I am displaying popup on mouse click anywhere on screen


Thanks

link publish delete flag offensive edit

answered 2012-01-03 12:40:09 +0800

carpolva gravatar image carpolva
155 4

Hi!...

Take a look at:

http://www.zkoss.org/zkdemo/popup/popup_position;jsessionid=F1BAFA4556D0D49D8EEDD9109DD5C4A9.zkdemo?search=popup

It may be helpful (see "positions" section).

Regards.

link publish delete flag offensive edit

answered 2012-01-03 16:08:12 +0800

mayurk gravatar image mayurk
21

hi carpolva,

Thanks for suggestion....actually tried all these but it wont help me.... I want to get an exact x y position of child component...

Regards,
Mayur

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2008-09-24 06:33:17 +0800

Seen: 2,416 times

Last updated: Jan 03 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More