Simulate a physical mouse move in Mac OS X

ぃ、小莉子 提交于 2019-12-12 07:52:12

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/3417970/simulate-a-physical-mouse-move-in-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!