开发者

Simulate a physical mouse move in Mac OS X

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开发者_StackOverflow (rather than pixels — that is important!)

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

Any ideas are welcome.

Thank you!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜