Capture Mouse Coordinates to variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbatestblrock
    New Member
    • Sep 2007
    • 164

    #1

    Capture Mouse Coordinates to variable

    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...


    Code:
    <!--
    ============================================================
    Capturing The Mouse Position in IE4-6 & NS4-6
    (C) 2000 www.CodeLifter.com
    Free for all users, but leave in this  header
    //-->
    
    <html>
    <body>
    
    <!-- Part One:
    Set up a form named "Show" with text fields named "MouseX"
    and "MouseY".  Note in the getMouseXY() function how fields
    are addressed, thus: document.FormName.FieldName.value
    //-->
    
    <form name="Show">
    <input type="text" name="MouseX" value="0" size="4"> X<br>
    <input type="text" name="MouseY" value="0" size="4"> Y<br>
    </form>
    
    <!-- Part Two:
    Use JavaScript ver 1.2 so older browsers ignore the script.
    The <script> must be *after* the <form> -- since the form
    and fields must exist *prior* to being called in the script.
    //-->
    
    <script language="JavaScript1.2">
    <!--
    
    // Detect if the browser is IE or not.
    // If it is not IE, we assume that the browser is NS.
    var IE = document.all?true:false
    
    // If NS -- that is, !IE -- then set up for mouse capture
    if (!IE) document.captureEvents(Event.MOUSEMOVE)
    
    // Set-up to use getMouseXY function onMouseMove
    document.onmousemove = getMouseXY;
    
    // Temporary variables to hold mouse x-y pos.s
    var tempX = 0
    var tempY = 0
    
    // Main function to retrieve mouse x-y pos.s
    
    function getMouseXY(e) {
      if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.body.scrollLeft
        tempY = event.clientY + document.body.scrollTop
      } else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX
        tempY = e.pageY
      }  
      // catch possible negative values in NS4
      if (tempX < 0){tempX = 0}
      if (tempY < 0){tempY = 0}  
      // show the position values in the form named Show
      // in the text fields named MouseX and MouseY
      document.Show.MouseX.value = tempX
      document.Show.MouseY.value = tempY
      return true
    }
    
    //-->
    </script>
    </body>
    </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.

    Code:
    counter = 0
    if counter < 20
    {
      if tempxy == grabxy
    
        set storexy cord
        wait 5 seconds
    
           if storexy == tempxy
           counter = counter ++
    }
    
    else
    //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!
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    //You don't need to know the coordinates to know if the mouse has moved.
    Code:
    function setdeadusertimer(){
        clearTimeout(window.deadusertimer);
        window.deadusertimer= setTimeout(function(){
            clearTimeout(window.deadusertimer);        
            document.body.onmousemove= 
            document.body.onkeypress= '';
            alert('Wake up!'); /*call or define function here*/        
        }, 60000);
    }
    
    window.onload= function(){
        document.body.onmousemove= 
        document.body.onkeypress= setdeadusertimer;
        setdeadusertimer();
    }

    Comment

    • mbatestblrock
      New Member
      • Sep 2007
      • 164

      #3
      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!

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

        #4
        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?

        Comment

        • mbatestblrock
          New Member
          • Sep 2007
          • 164

          #5
          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!!!

          Comment

          Working...