473,895 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Capture Mouse Coordinates to variable

164 New Member
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. //-->
  8. <html>
  9. <body>
  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. //-->
  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>
  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. //-->
  28. <script language="JavaScript1.2">
  29. <!--
  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
  35. // If NS -- that is, !IE -- then set up for mouse capture
  36. if (!IE) document.captureEvents(Event.MOUSEMOVE)
  38. // Set-up to use getMouseXY function onMouseMove
  39. document.onmousemove = getMouseXY;
  41. // Temporary variables to hold mouse x-y pos.s
  42. var tempX = 0
  43. var tempY = 0
  45. // Main function to retrieve mouse x-y pos.s
  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. }
  65. //-->
  66. </script>
  67. </body>
  68. </html>
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
  6.     set storexy cord
  7.     wait 5 seconds
  9.        if storexy == tempxy
  10.        counter = counter ++
  11. }
  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 3531
428 Contributor
//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. }
  11. window.onload= function(){
  12.     document.body.onmousemove= 
  13.     document.body.onkeypress= setdeadusertimer;
  14.     setdeadusertimer();
  15. }
Jan 8 '09 #2
164 New Member
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
428 Contributor
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
164 New Member
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

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

Similar topics

by: Marcia Gulesian | last post by:
How can I capture the event when I click (focus) with the cursor anywhere in the page (that is, on a component or elsewhere). This event would occur in an I.E 5.5 or later browser.
by: Danny | last post by:
How can I get the coordinates of the mouse cursor in Mozilla browsers as well as Opera and IE6? I'm struggling to understand how to capture mouse movement events with Mozilla/Netscape/Firefox and I've Googled so much my brain hurts. http://www.ghpkendal.co.uk/TestPages/Test.htm Move your cursor over the yellow area and you should see the mouse coordinates echoed above.
by: Jay | last post by:
Hi, How can I capture mouse position on Image? I found number of script capturing mouse position of the page. But I could not find anything based on image. What I want to find out is X Y coordinates of mouse position. based on left of the top of my image is 0 0 (X Y coordinates) otherwise, I need to find out position of my image so I can calculate.
by: Lars Netzel | last post by:
Hi! I have a round area which I want to be able to move the mouse over and fire off events... how do I do that? I have drawn a FillPie Graphics and I feel that there has to be a way of getting to know if the mouse is over that area since I have the coordinates to paint the Pie but I don't know where to start or what to look for really. Best Regard
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and display (in the status bar) the image coordinates of the mouse location. However, if I use the picturebox's MouseMove event, I am getting the coordinates of the mouse over the PICTUREBOX, not the actual image underneath that (which is stretched)....
by: quickcur | last post by:
Hi, I have html like this: <div id="myCanvas" style="border:10px, black;position:relative;height:250px;width:100%;"> <img id="p" src="p.jpg"> </div> When user click the mosue, I would like 1. Test if the mouse location is on top of the image
by: TC | last post by:
Hello, I need to build a very simple text editor. The requirement is that the input screen should be divided into 'm*n' cells ('m' rows, 'n' columns, with each cell of a fixed size). Whenever the user wants to input text, he will click on one of these cells, and then enter the text. The text should then be visible inside the cell. From my very limited knowledge of VB, I plan to use the 'group box' construct to denote one cell. Now,...
by: cefrancke | last post by:
Is there a way to capture all relevant info about the mouse, without using Mouse Up/Down etc. procedures? I'm trying to make a custom function and send it the mouse info on the click event. For example:
by: =?Utf-8?B?Unlhbg==?= | last post by:
I have a winform containing a scrollable panel and a groupbox inside the panel. There is a button inside the groupbox. When that button is clicked; how do I capture and display the X and Y coordinates in a messagebox?
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.