views:

104

answers:

1

Hello, I'm looking for a way to simulate a mouse move event in Mac OS X 10.6. It would have to be defined in mouse units (rather than pixels — that is important!)

I need this for an experiment which basically consists of drawing lines.

Any ideas are welcome.

Thank you!

A: 

One of the easiest ways to move the mouse in Mac OS X and other operating systems is to use a Java Robot. It can also simulate other events. For example, the mouse down or even a key press. However, it moves the pointer to a given screen coordinates. So the only thing you need to do is to convert your physical units into appropriate coordinates. Here is a code example:

import java.awt.AWTException;
import java.awt.Robot;

public final class JavaRobotExample
{
    public static void main(String[] args) throws AWTException
    {
    Robot robot = new Robot();

    robot.setAutoDelay(5);
    robot.setAutoWaitForIdle(true);

    robot.mouseMove(0, 0);
    robot.delay(1000);
    robot.mouseMove(200, 10);
    robot.delay(1000);
    robot.mouseMove(40, 130);

    System.exit(0);
    }
}

To test this code, put it into JavaRobotExample.java file, then compile it using the following command:

javac JavaRobotExample.java

Once JavaRobotExample.class file is produced, run it:

java JavaRobotExample

Java runtime comes with Mac OS X by default. I am not sure about the SDK (compiler) though. If you don't have a javac command, simply install Xcode.

Vlad Lazarenko
The problem is, I'm testing Mac mouse handling (and acceleration) and I want the system to think that I moved my mouse — to see how it moves the cursor.I suppose this is some lower level stuff. I know it's possible in Windows by using "nircmdc" with the command "sendmouse move x y".Thank you for the code though, it'll come in handy for the other experiment! :-)
Dae
@Dae In this case you need to write your HID driver or hook into existing one. There is one open source Xbox HID driver for OSX you can use as an example - http://xhd.cvs.sourceforge.net/viewvc/xhd/xhd/
Vlad Lazarenko