/    Sign up×
Community /Pin to ProfileBookmark

Monitor Mouse Position

I have two problems with the following code. The program works fine, except:

  • 1. I want to be able to monitor the position of the mouse for a period of time (10 seconds).
    Currently the program works, but only displays changes to the mouse position
    when the mouse has been moved. I want to save the mouse position at all
    times after the ‘start’ button has been pressed.
  • My goal: I would like to monitor the X,Y position at one second intervals,
    even when the mouse is not moved.
    I assume I will need to look at the system clock and force an event to occur
    whenever one second has passed. Anyone know of the function to call to
    get this to happen?

  • 2. When I un-comment the <form>…</form> tags in the code,
    the script no longer works as without them!
    Anyone have an idea why this is happening?
  • I will need the program to work with the <form> tags in so that I can send
    the acquired information to a server file. The ‘Clear’ button performs the
    functions of the ‘Reset’ until I can get the script to work as desired.

    Any help is appreciated.

    [code=php]

    <html>
    <head>
    <title>Follow Mouse</title>

    <style type=”text/css”>
    #mydiv {
    position:absolute;
    overflow:hidden;
    visibility: hidden;
    z-index:999;
    width:40px; height:20px;
    background-color:#ffffcc;
    }
    </style>

    <script type=”text/javascript”>
    <!–
    var XYClock = 0;
    var StartXYClock = 0;

    function SaveXYpos() {
    // save X,Y mouse position once per second
    document.getElementById(‘PosList’).value
    += document.getElementById(‘Xpos’).value + ‘,’ + document.getElementById(‘Ypos’).value + “n”;
    }

    function StartCollection() {
    // start using system clock to check mouse position
    XYClock = 1;
    var dtime = new Date(); var dsec = dtime.getTime();
    StartXYClock = dsec;
    // document.onmousemove = follow; // doesn’t work as expected
    }

    function StopCollection() {
    // stop using system clock to check mouse position
    XYClock = 0;
    // document.onmousemove = void(); // doesn’t work as expected
    }

    // Simple follow the mouse script

    var divName = ‘mydiv’; // div that is to follow the mouse
    // (must be position:absolute)
    var offX = 15; // X offset from mouse position
    var offY = 15; // Y offset from mouse position

    function mouseX(evt) {
    if (!evt) evt = window.event;
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
    return evt.clientX
    + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    else return 0;
    }
    function mouseY(evt) {
    if (!evt) evt = window.event;
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
    return evt.clientY
    + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    else return 0;
    }

    function follow(evt) {
    if (document.getElementById) {
    var obj = document.getElementById(divName).style;
    obj.visibility = ‘visible’;
    obj.left = (parseInt(mouseX(evt))+offX) + ‘px’;
    obj.top = (parseInt(mouseY(evt))+offY) + ‘px’;
    document.getElementById(‘Xpos’).value = mouseX(evt);
    document.getElementById(‘Ypos’).value = mouseY(evt);
    if (XYClock == 0) { return }
    var dtime = new Date(); var dsec = dtime.getTime();
    if (dsec > (XYClock+1000)) { SaveXYpos(); XYClock = dsec; }
    if (dsec > (StartXYClock+(1000*10))) { XYClock = 0; }
    }
    }

    function ClearAll() {
    document.getElementById(‘Xpos’).value = 0;
    document.getElementById(‘Ypos’).value = 0;
    document.getElementById(‘PosList’).value = ”;
    XYClock = 0; StartXYClock = 0;
    }

    document.onmousemove = follow;
    //–>
    </script>
    </head>

    <body>
    <!– form id=”spotform” –>
    <button id=”btnStart” onClick=”StartCollection()”>Start</button>
    <button id=”btnStop” onClick=”StopCollection()”>Stop</button>
    <br />
    <div> Xpos: <input id=”Xpos” type=”text” value=0 size=1>
    Ypos: <input id=”Ypos” type=”text” value=0 size=1>
    <br /><textarea id=”PosList” cols=”10″ rows=”20″</textarea>
    <div id=”mydiv”>X,Y</div>
    <br />
    <input type=”submit” value=”Submit” />
    <input type=”reset” value=”Reset” />
    <button onClick=”ClearAll()”>Clear</button> <!– use this until ‘form’ works –>
    <!– /form –>
    </body>
    </html>

    [/code]

    One last question, once I get the <form> tags to work:
    Can I force a submit to occur when the operator presses the ‘Reset’ button
    OR
    if the operator does a reload of the page (using Ctrl-F5)?

    to post a comment
    JavaScript

    7 Comments(s)

    Copy linkTweet thisAlerts:
    @KorJul 30.2006 — Here's a code which will return the mouse position whithin a certain interval (in my code is set to 3 seconds, but you may adjust the interval at will
    [code=php]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/JavaScript">
    var posx;var posy;
    var del =3000; //the interval in miliseconds
    var q=true
    function getMouse(e){
    posx=0;posy=0;
    var ev=(!e)?window.event:e;//Moz:IE
    if (ev.pageX){//Moz
    posx=ev.pageX+window.pageXOffset;
    posy=ev.pageY+window.pageYOffset;
    }
    else if(ev.clientX){//IE
    posx=ev.clientX+document.body.scrollLeft;
    posy=ev.clientY+document.body.scrollTop;
    }
    else{return false}//old browsers
    q?showPos():null
    }
    function showPos(){
    document.getElementById('mydiv').firstChild.data='X='+posx+' Y='+posy;
    q=q?!q:q;
    setInterval('showPos()',del)
    }
    </script>
    </head>
    <body onmousemove="getMouse(event)">
    <div id="mydiv">&nbsp;</div>
    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @JMRKERauthorJul 30.2006 — Thanks KOR, ?

    Looks like I need to learn about 'setInterval()' .

    Thanks for putting me onto a path to search.

    I execute your code and it appears to do what you say for a brief period of time.

    The only problem I'm having with it is that it does not appear to let me exit.

    When I run the program it displays the mouse positions for some period of time (not determined yet)

    and then just stops responding to the mouse movements. ?

    I'm using FF browser and I must cancel the browser to exit the program. (?)

    I'll keep looking into my problem of accumulating the mouse position information over a period of time. :o
    Copy linkTweet thisAlerts:
    @KorJul 30.2006 — Mybe this?
    [code=php]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/JavaScript">
    var posx;var posy;
    var q=false;
    function getMouse(e){
    if(q){
    posx=0;posy=0;
    var ev=(!e)?window.event:e;//Moz:IE
    if (ev.pageX){//Moz
    posx=ev.pageX+window.pageXOffset;
    posy=ev.pageY+window.pageYOffset;
    }
    else if(ev.clientX){//IE
    posx=ev.clientX+document.body.scrollLeft;
    posy=ev.clientY+document.body.scrollTop;
    }
    else{return false}//old browsers
    document.getElementById('mydiv').firstChild.data='X='+posx+' Y='+posy;
    }
    }

    </script>
    </head>
    <body onmousemove="getMouse(event)">
    <div id="mydiv">&nbsp;</div>
    <button id="btnStart" onClick="q=true">Start</button>
    <button id="btnStop" onClick="q=false">Stop</button>
    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @JMRKERauthorJul 31.2006 — The last code from KOR works fine. I incorporated it into what I am trying to do.

    ? ? ?

    Try code below as is. Works fine to collect mouse position data every 1 second

    over a 10 second period. Puts data into <textarea> section.

    The 'Clear' button resets display so you can do it again.
    <i>
    </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
    &lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
    &lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
    &lt;title&gt;Follow Mouse&lt;/title&gt;

    &lt;style type="text/css"&gt;
    #mydiv {
    position:absolute;
    overflow:hidden;
    visibility: hidden;
    z-index:999;
    width:40px; height:20px;
    background-color:#ffffcc;
    }
    &lt;/style&gt;

    &lt;script type="text/javascript"&gt;
    &lt;!--
    var XYClock = 0;
    var StartXYClock = 0;
    var timed = 0;

    function SaveXYpos() {
    // save X,Y mouse position once per second
    document.getElementById('PosList').value
    += document.getElementById('Xpos').value + ',' + document.getElementById('Ypos').value + "n";
    }

    function StartCollection() {
    // start using system clock to check mouse position
    XYClock = 1;
    var dtime = new Date(); var dsec = dtime.getTime();
    StartXYClock = dsec;
    timed = window.setInterval("SaveXYpos()",1000);
    }

    function StopCollection() {
    // stop using system clock to check mouse position
    XYClock = 0;
    var times = window.clearInterval(timed);
    }

    // Simple follow the mouse script

    var divName = 'mydiv'; // div that is to follow the mouse
    // (must be position:absolute)
    var offX = 15; // X offset from mouse position
    var offY = 15; // Y offset from mouse position

    function mouseX(evt) {
    if (!evt) evt = window.event;
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
    return evt.clientX
    + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    else return 0;
    }
    function mouseY(evt) {
    if (!evt) evt = window.event;
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
    return evt.clientY
    + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    else return 0;
    }

    function follow(evt) {
    if (document.getElementById) {
    var obj = document.getElementById(divName).style;
    obj.visibility = 'visible';
    obj.left = (parseInt(mouseX(evt))+offX) + 'px';
    obj.top = (parseInt(mouseY(evt))+offY) + 'px';
    document.getElementById('Xpos').value = mouseX(evt);
    document.getElementById('Ypos').value = mouseY(evt);
    if (XYClock == 0) { return }

    <i> </i>var dtime = new Date(); var dsec = dtime.getTime();
    <i> </i>if (dsec &gt; (StartXYClock+(1000*10))) { StopCollection(); }
    }
    }

    function ClearAll() {
    document.getElementById('Xpos').value = 0;
    document.getElementById('Ypos').value = 0;
    document.getElementById('PosList').value = '';
    XYClock = 0; StartXYClock = 0;
    }

    document.onmousemove = follow;
    //--&gt;
    &lt;/script&gt;
    &lt;/head&gt;

    &lt;body&gt;
    &lt;!-- form id="spotform" --&gt;
    &lt;button id="btnStart" onClick="StartCollection()"&gt;Start&lt;/button&gt;
    &lt;button id="btnStop" onClick="StopCollection()"&gt;Stop&lt;/button&gt;
    &lt;br /&gt;
    &lt;div&gt; Xpos: &lt;input id="Xpos" type="text" value=0 size=1&gt;
    Ypos: &lt;input id="Ypos" type="text" value=0 size=1&gt;
    &lt;br /&gt;&lt;textarea id="PosList" cols="10" rows="20"&lt;/textarea&gt;
    &lt;div id="mydiv"&gt;X,Y&lt;/div&gt;
    &lt;br /&gt;
    &lt;input type="submit" value="Submit" /&gt;
    &lt;input type="reset" value="Reset" /&gt;
    &lt;button onClick="ClearAll()"&gt;Clear&lt;/button&gt; &lt;!-- use this until 'form' works --&gt;
    &lt;!-- /form --&gt;
    &lt;/body&gt;
    &lt;/html&gt;


    ? ? ?

    HOWEVER, if you remove the comments around
    <i>
    </i>&lt;!-- form id="spotform" --&gt;

    ...

    &lt;!-- /form --&gt;

    Near the end of the script, change to:
    <i>
    </i>&lt;form id="spotform"&gt;

    ...

    &lt;/form&gt;


    I get errors depending upon the browser I am testing with.

    For FF, it stops saving mouse positions, but the location boxes continue to change in FF.

    I am not getting any errors in the 'javascript:' console with FF.

    For IE, it continues to save mouse positions (0,0) after displaying some gibberish

    in the <textarea>, but the mouse positions stay zero ???

    However, I am getting errors in IE stating:

    var obj = document.getElementById(divName).style;

    this line has an 'object required', but I don't know what's missing.


    Anyone have any idea WHY this is happening or what I am doing wrong?

    Why can't we have one script that works in ALL browsers the same???

    Seems like there is always something wrong twice for each error found.
    Copy linkTweet thisAlerts:
    @KorJul 31.2006 — At a first glance, a HTML error:

    <textarea id="PosList" cols="10" rows="20"[COLOR=Red]>[/COLOR]</textarea>
    Copy linkTweet thisAlerts:
    @JMRKERauthorJul 31.2006 — Good eyes KOR. I totally missed that!

    Too busy trying to fix my problems I guess I was looking for something harder!

    I'll give it a try to see it it solved my problems.

    I'm not sure why I did not get the same effect with FF (no errors and no display in textarea) when I was using the same script, but at least I'm one error less and closer to the final solution.

    Thanks. I'll report back about any other problems, if they continue.
    Copy linkTweet thisAlerts:
    @James_GatkaJul 31.2006 — = == ==
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...