For this shortened Fourth of July week, I turned to Java to
develop a program capable of logging mouse events. One advantage of using Java
is that it is intrinsically platform-independent since each Java application is
compiled into bytecode that is then executed in the Java Runtime Environment. I
used the java.awt.MouseInfo class to sample the x and y coordinates of the
mouse ten times per second. If the position of the mouse has not changed since
the last sample, it is not logged again. The code ended up looking like this:
while(true){
Point mousePt = MouseInfo.getPointerInfo().getLocation();
mouseX = Math.max(0, mousePt.x);
mouseY = Math.max(0, mousePt.y);
if (mouseX != prevX || mouseY != prevY){
unixTime = System.currentTimeMillis();
delta = unixTime - prevTime;
String content = unixTime + " " + delta +
" [" + mouseX + "," + mouseY + "]\n";
writeToFile(filename, content);
}
try { Thread.sleep(100); }
catch (InterruptedException exception){
exception.printStackTrace();
throw exception;
}
prevX = mouseX;
prevY = mouseY;
prevTime = unixTime;
}
The log file I create also includes the timestamp with
millisecond granularity as well as the delta since the last different pair of x
and y coordinates. This program should work for all three of the platforms we
have targeted for this project.
No comments:
Post a Comment