Skip to main content
October 9, 2015
Answered

How can I trace mouse moves and ignore non moves

  • October 9, 2015
  • 1 reply
  • 1647 views

I have a program that records the mouse position every 300 msec over an approximately 9 minute  time period.

However, I need to know the mouse position ONLY when it is not the same as the previous moment (sometimes the user stops to talk. I don't want to record 5 minutes of the same mouse position)

Here's the original program

var drawTimer:Timer = new Timer(300, 500000);

    drawTimer.addEventListener(TimerEvent.TIMER, timeSampler);

   

       function timeSampler(evt:TimerEvent):void {

   

           { trace (+mouseX, "," +mouseY);

      }

}

     

I tried creating a variable to capture the mouse position at a specific moment

     var posMouseX:Number= mouseX;

     var posMouseY:Number= mouseY;

and to add a conditional statement into the function

    if ((mouseX != posMouseX) && (mouseY != posMouseY))
                { trace (+mouseX, "," +mouseY);}

but I am  not doing this correctly because I either get all mouse moves or none

Can anyone suggest a better way to do this?

Thanks in advance

This topic has been closed for replies.
Correct answer nezarov

The above code can do what you're looking for

you'll get the values only when the mouse is moving, so there will not be duplicated values

stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);

function onMove(e:MouseEvent):void

{

    trace(mouseX + ", " + mouseY);

     // add your code here to record the values, for example:

     // myArray.push(mouseX, mouseY);

}

as the timer still counting down and on complete it'll remove the stage event listener.

var drawTimer:Timer = new Timer(300, 500000);

drawTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeSampler);

function timeSampler(evt:TimerEvent):void

{

     stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);

}

drawTimer.start();

1 reply

Inspiring
October 9, 2015

use MouseEvent.MUSE_MOVE

var drawTimer:Timer = new Timer(300, 500000);

drawTimer.addEventListener(TimerEvent..TIMER_COMPLETE, timeSampler); // here you have to use TIMER_COMPLETE not TIMER cause TIMER will call the function each 300 msec means the mouse event listener will be removed after 300 msec only.

stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);

function onMove(e:MouseEvent):void

{

    //trace (+mouseX, "," +mouseY); //in your code there's a comma after mouseX you have to remove it.

    trace(mouseX + ", " + mouseY);

}

// on time complete remove the event listener

function timeSampler(evt:TimerEvent):void

{

     stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);

}

drawTimer.start();

October 17, 2015

Thank you.

I'm trying to follow the logic.

The software is trying to capture the effort to locate a hidden target by placing the mouse in the correct location.

As each mouse move is made, it is (1) traced into the output window and (2) uploaded into an Excel  file to be looked at later (this is why the comma is present)

The timer samples his position every 300 msec

Sometimes, the person doing the task will stop to talk for 2 minutes.

But the software is sampling mouse position every 300 msec the whole time and the Excel file will contain a hundred repeats of the same mouse position.

So I don't need these duplicate values  I want to get rid of them before they get put into the Excel matrix

But I don't want to stop the timer.

Once the person resumes the search, I still want to record the mouse moves

Thanks

nezarovCorrect answer
Inspiring
October 17, 2015

The above code can do what you're looking for

you'll get the values only when the mouse is moving, so there will not be duplicated values

stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);

function onMove(e:MouseEvent):void

{

    trace(mouseX + ", " + mouseY);

     // add your code here to record the values, for example:

     // myArray.push(mouseX, mouseY);

}

as the timer still counting down and on complete it'll remove the stage event listener.

var drawTimer:Timer = new Timer(300, 500000);

drawTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeSampler);

function timeSampler(evt:TimerEvent):void

{

     stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);

}

drawTimer.start();