## Tracking Mouse Speed

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Jakell
Posts: 129
Joined: Thu Mar 08, 2007 5:39 am UTC
Contact:

### Tracking Mouse Speed

Hey Folks-

I am putting together an experiment which watched the brownian motion of suspended glue molecules, but to get any good numbers from it, I need a Mac osX program which records the speed at which the mouse pointer twitches around. Does anyone have such a program, or could anyone point me in the direction of such a program? Thanks much!
Physics Rocks!
Indigo is Bullshit.

Why not?

If you can't hear your heart pounding in your ears several times a day, you aren't dong it right.

jimrandomh
Posts: 110
Joined: Sat Feb 09, 2008 7:03 am UTC
Contact:

### Re: Tracking Mouse Speed

Recording mouse movement is pretty trivial, but I'm having trouble seeing how that could make for a good experiment. If you're converting instrument data into mouse motion, you'll find that mouse acceleration and the edges of the screen mess you up. If you're giving cues and asking a user to do something with the mouse, the result will depend heavily on the individual user. In any case, you'll have to write the program yourself, since the low-level details of the accounting matter.

laranzu
Posts: 104
Joined: Fri Nov 30, 2007 4:21 pm UTC
Location: Physical: Canberra, Australia
Contact:

### Re: Tracking Mouse Speed

First, is this a program you are writing yourself in C/Cocoa/other language? If so, it should be easy: just grab the mouse position each time through your event loop.

Or do you need a program which can generate mouse positions as some kind of output file, or network data stream? If so, the big question is how often do you need to sample the mouse?

Second, what is the mouse position being used for? Is it a source of random numbers, or is it actually the data you want to record through some kind of system that uses Brownian motion to trigger a mouse camera or something? (If so, please provide details, this is exactly the kind of wierd gadget that XKCD folk would find fascinating!)

If you can provide this info, I'll (probably) be happy to help.

sakeniwefu
Posts: 170
Joined: Sun May 11, 2008 8:36 pm UTC

### Re: Tracking Mouse Speed

I don't know Mac OS X, it could be that you can get speed from some API. If you can't, I doubt there is any library either because it's trivial. You poll the mouse position each t seconds. That should give you X and Y. Poll again. Divide distance by time and you have speed in pixels per second.
$Yspeed=\frac{Y-Yo}{t}$

trebach
Posts: 22
Joined: Wed Jun 04, 2008 1:36 am UTC

### Re: Tracking Mouse Speed

sakeniwefu wrote:I don't know Mac OS X, it could be that you can get speed from some API. If you can't, I doubt there is any library either because it's trivial. You poll the mouse position each t seconds. That should give you X and Y. Poll again. Divide distance by time and you have speed in pixels per second.
$Yspeed=\frac{Y-Yo}{t}$

You're probably going to want it regardless of direction, so speed regardless would be:
$Speed=\frac{\sqrt{(X-Xo)^2 + (Y-Yo)^2}}{t}$

Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

### Re: Tracking Mouse Speed

I don't know for Mac OSX, but in the Windows API it's called "raw mouse input" - which is exactly what its name implies. You get data straight from the mouse driver, which comes in as relative mouse movement (how far the mouse moved since last poll time). This is more robust than manually polling the mouse position, because the mouse can get "stuck" on the sides of the screen. I'm sure Mac OSX would have similar functionality.

Jakell
Posts: 129
Joined: Thu Mar 08, 2007 5:39 am UTC
Contact:

### Re: Tracking Mouse Speed

Sorry for not getting back quickly enough, out of town.

If you look at an optical mouse, there are two lights coming from it, the big bright one, and a smaller one that points out where the ...camera... is looking. If you shine a laser pointer on a surface, you can actually aim that small light at the spot, and move the mouse from that point of light; several decimeters away if it is a bright laser. If the laser is shined on something kinda moving, like a bit of white glue, then the mouse see's the shifting interference pattern, and skips around, mimicking the brownian motion of the glue molecules.

I would like the data to either be fed out in a file (simple x vs. Y position would be fine, I could calculate velocity from that) or displayed as a vector graph; if it moved to the left a few centimeters, we'd get a point like (-3, 0). And the mouse moves pretty fast, about as fast as it can for really hto stuff, so the highest data rate would be wonderful.

It's actually fairly entertaining. Once I got it working, I spent an hour just watching the mouse jitter around the screen. You can get speed changes if you heat the glue, water it down, let it dry,- I find it utterly fascinating.
Physics Rocks!
Indigo is Bullshit.

Why not?

If you can't hear your heart pounding in your ears several times a day, you aren't dong it right.

Psuedo_Bob
Posts: 40
Joined: Fri May 30, 2008 4:26 am UTC
Contact:

### Re: Tracking Mouse Speed

That's actually the most awesome thing I have heard of in quite some time.
Yes, I know how to spell "Pseudo". It's, eh... art. Or something. It's an artistic choice.

http://thek3vin.deviantart.com/
I draw comics. They look kinda like xkcd. But I promise they're not.

Ended
Posts: 1459
Joined: Fri Apr 20, 2007 3:27 pm UTC
Location: The Tower of Flints. (Also known as: England.)

### Re: Tracking Mouse Speed

The following java snippet will listen for mouse movement within a window and append the new (x, y) coordinates to a file out.txt. I'm not sure what the data rate will be like, though. Anyway, you might be able to modify it to suit your needs.

(Save in a file called "GetMouseMotion.java", compile with "javac GetMouseMotion.java", run with "java GetMouseMotion").

Spoiler:

Code: Select all

import java.awt.event.*;import java.io.*;import javax.swing.*;public class GetMouseMotion{    public static void main(String[] args)    {   JFrame frame = new JFrame();   frame.setSize(500, 500);   frame.addMouseMotionListener(new MyMouseMotionListener());   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   frame.setVisible(true);    }    static class MyMouseMotionListener implements MouseMotionListener    {   FileWriter fstream;   BufferedWriter out;   public MyMouseMotionListener()   {       try       {      fstream = new FileWriter("out.txt");       }       catch (IOException ex){}       out = new BufferedWriter(fstream);   }   public void mouseMoved(MouseEvent e)   {       try       {      out.write(e.getX() + " " + e.getY());      out.newLine();      out.flush();       }       catch (IOException ex){}   }   public void mouseDragged(MouseEvent e){}    }}

Psuedo_Bob wrote:That's actually the most awesome thing I have heard of in quite some time.

I agree - definitely going to try this at some point.
Generally I try to make myself do things I instinctively avoid, in case they are awesome.
-dubsola

Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

### Re: Tracking Mouse Speed

I did a quick test with that code, except I added timestamps to the output, and it seems I'm getting about 400Hz (~400 lines in ~1 second), which is what my mouse's currently set to. It goes as high as 2000Hz, but I don't have the firmware installed on my computer right now so I can't change the update frequency to test.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

wr3cktangle
Posts: 75
Joined: Tue Aug 01, 2006 5:03 pm UTC

### Re: Tracking Mouse Speed

How did this experiment come to be, and is it possible to get a picture of your setup?
Maxim 33. When faced with the unusual, self-destruct.
My blog

Jakell
Posts: 129
Joined: Thu Mar 08, 2007 5:39 am UTC
Contact:

### Re: Tracking Mouse Speed

Ended- Thanks for the code, I am compiling as we speak! I will let you folks know of my success.

It was figured out in a not too exciting fashion for most onlookers; it was over a discussion with my boss about how mouses work, and I have watched the brownian motion of glue molecules for a while now; these two ideas came to together real well for me.

I have attached a few pics of my basic setup, note that it is the small red light which is hitting the 5mW laser dot, not the big, obvious one. You can move the mouse around, and as long as the small light is still on the laser, it should work. This works up to about two feet for me, but I have used a 150mW laser, and gotten it to work from several feet away. Red laser light works as well, but green is so much cooler! In theory, if you have a bright laser spread out over a surface, or perhaps several lasers in a grid over an area, you could use the mouse without touching the surface at all.

To get brownian motion, just put a some liquid in front of the beam, and in the small red light. I have a few small, clear test-tubes that I have put glue into, though milk also works quite well. I am, at some point, going to play around with the temperature dependence too; hotter liquid should move the pointer around much more. I think the milk molecules are much smaller then the glue, so they should be moving faster as well.

Spoiler:
DSCI0089 copy.jpg (108.71 KiB) Viewed 4730 times
DSCI0095.jpg (118.83 KiB) Viewed 4735 times
Physics Rocks!
Indigo is Bullshit.

Why not?

If you can't hear your heart pounding in your ears several times a day, you aren't dong it right.

Ended
Posts: 1459
Joined: Fri Apr 20, 2007 3:27 pm UTC
Location: The Tower of Flints. (Also known as: England.)

### Re: Tracking Mouse Speed

Did you manage to get this working in the end?
Generally I try to make myself do things I instinctively avoid, in case they are awesome.
-dubsola

Jakell
Posts: 129
Joined: Thu Mar 08, 2007 5:39 am UTC
Contact:

### Re: Tracking Mouse Speed

The experiment ended up working out great for my class. I am not too java-handy, and so was not able to directly graph the data out, but I the raw count of how much the mouse moved over time works great; The time it takes to move a distance total is proportional to the size of the molecules in the liquid, and to the temperature of the liquid itself!

The final setup had students putting a few drops of milk, or watered glue, on a peltier device, letting them vary the liquid between freezing to boiling, with the push of a button. I played around with having the milk/glue extremely watered down (in the hopes of catching individual molecules, not just the ensemble), and shining the laser through the water, but ever really got a good result off it; there should be a way, though.

Work, of course, kept on piling up, so I have not been able to take another crack at working with the code, but thanks much for the good start! Should I be able to make real progress, I will post it here. Thankyou again!
Physics Rocks!
Indigo is Bullshit.

Why not?

If you can't hear your heart pounding in your ears several times a day, you aren't dong it right.