469,640 Members | 1,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

Capture Mouse Coordinates to variable

164 100+
I hope this makes some sense. My ultimate goal here is to execute a block of code if the mouse has not moved in a minute or so within the broswer. The machine I am running this on is for internal use only, so I have no fear of this not working in certain browsers, etc.

I am no javascript pro but know enough to catch on if anyone is kind enough to help me out here. One of the only ways I could see this working is by having js check the mouse coordinates and compare them to see if they have changed or not. I dont know if this is an absurd way to go about or not, but I could see it working. So I have this so far, it just shows the x and y coordiantes...


Expand|Select|Wrap|Line Numbers
  1. <!--
  2. ============================================================
  3. Capturing The Mouse Position in IE4-6 & NS4-6
  4. (C) 2000 www.CodeLifter.com
  5. Free for all users, but leave in this  header
  6. //-->
  7.  
  8. <html>
  9. <body>
  10.  
  11. <!-- Part One:
  12. Set up a form named "Show" with text fields named "MouseX"
  13. and "MouseY".  Note in the getMouseXY() function how fields
  14. are addressed, thus: document.FormName.FieldName.value
  15. //-->
  16.  
  17. <form name="Show">
  18. <input type="text" name="MouseX" value="0" size="4"> X<br>
  19. <input type="text" name="MouseY" value="0" size="4"> Y<br>
  20. </form>
  21.  
  22. <!-- Part Two:
  23. Use JavaScript ver 1.2 so older browsers ignore the script.
  24. The <script> must be *after* the <form> -- since the form
  25. and fields must exist *prior* to being called in the script.
  26. //-->
  27.  
  28. <script language="JavaScript1.2">
  29. <!--
  30.  
  31. // Detect if the browser is IE or not.
  32. // If it is not IE, we assume that the browser is NS.
  33. var IE = document.all?true:false
  34.  
  35. // If NS -- that is, !IE -- then set up for mouse capture
  36. if (!IE) document.captureEvents(Event.MOUSEMOVE)
  37.  
  38. // Set-up to use getMouseXY function onMouseMove
  39. document.onmousemove = getMouseXY;
  40.  
  41. // Temporary variables to hold mouse x-y pos.s
  42. var tempX = 0
  43. var tempY = 0
  44.  
  45. // Main function to retrieve mouse x-y pos.s
  46.  
  47. function getMouseXY(e) {
  48.   if (IE) { // grab the x-y pos.s if browser is IE
  49.     tempX = event.clientX + document.body.scrollLeft
  50.     tempY = event.clientY + document.body.scrollTop
  51.   } else {  // grab the x-y pos.s if browser is NS
  52.     tempX = e.pageX
  53.     tempY = e.pageY
  54.   }  
  55.   // catch possible negative values in NS4
  56.   if (tempX < 0){tempX = 0}
  57.   if (tempY < 0){tempY = 0}  
  58.   // show the position values in the form named Show
  59.   // in the text fields named MouseX and MouseY
  60.   document.Show.MouseX.value = tempX
  61.   document.Show.MouseY.value = tempY
  62.   return true
  63. }
  64.  
  65. //-->
  66. </script>
  67. </body>
  68. </html>
  69.  
  70.  
I know the following is not correct at all, but I was just sketching the idea out in notepad for how the logic could work.

Expand|Select|Wrap|Line Numbers
  1. counter = 0
  2. if counter < 20
  3. {
  4.   if tempxy == grabxy
  5.  
  6.     set storexy cord
  7.     wait 5 seconds
  8.  
  9.        if storexy == tempxy
  10.        counter = counter ++
  11. }
  12.  
  13. else
  14. //code to go here
I dont even know if that is decipherable or even close to the correct syntax? But my MAIN question is this.. If someone can help me with this, I am pretty certain I can make this work.

How would I "capture" the tempX and tempY variables and store them in say, var Capture1 and then 5 seconds later "capture" tempX and tempY again, and store those values in say, var Capture2? If I can grab the coordinates at a given time I can then compare them. If the coordiantes stay the same for about 10 loops thorough that code, then my next block of code could be run.


... It is late.... I hope this makes sense, and I would be SUPER happy if someone could help me out, or at least point me somewhere.

Thanks a ton for any help!
Jan 8 '09 #1
4 3273
mrhoo
428 256MB
//You don't need to know the coordinates to know if the mouse has moved.
Expand|Select|Wrap|Line Numbers
  1. function setdeadusertimer(){
  2.     clearTimeout(window.deadusertimer);
  3.     window.deadusertimer= setTimeout(function(){
  4.         clearTimeout(window.deadusertimer);        
  5.         document.body.onmousemove= 
  6.         document.body.onkeypress= '';
  7.         alert('Wake up!'); /*call or define function here*/        
  8.     }, 60000);
  9. }
  10.  
  11. window.onload= function(){
  12.     document.body.onmousemove= 
  13.     document.body.onkeypress= setdeadusertimer;
  14.     setdeadusertimer();
  15. }
Jan 8 '09 #2
mbatestblrock
164 100+
Thanks for the reply! Your probably right, I may not need to capture the x-y coordinates, it was just the only thing I could come up with. I tired your code you posted, and it almost seems like it would work, and maybe I am missing something here. But when I try it out no matter if I move my mouse or not it launches the wake up alert. Am I missing something???

Thank you so much!
Jan 8 '09 #3
mrhoo
428 256MB
You got me- if it works at all, it should clear the timeout and set a new one on a mouse move or key press. I could guess if it did nothing...

Let me know if you figure it out- I can't make it fail, but I don't know your set up.

Maybe try setting the handlers on the window or the document instead of the body, or put a doctype on the page if it has none- I'm just guessing, but maybe it gets the method from the initial call and doesn't 'hear' the event from the body- you aren't handling mousemoves from anywhere else, are you?
Jan 8 '09 #4
mbatestblrock
164 100+
It Works!

I have NO idea what was wrong earlier that had made it launch no matter what. I seriously cannot thank you enough. This was obviously a much better implementation than what I was trying to do.

Thanks again!!!
Jan 9 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Marcia Gulesian | last post: by
10 posts views Thread by Danny | last post: by
4 posts views Thread by Jay | last post: by
13 posts views Thread by Lars Netzel | last post: by
2 posts views Thread by quickcur | last post: by
4 posts views Thread by =?Utf-8?B?Unlhbg==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.