本文介绍了模拟Mac OS X中的物理鼠标移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在Mac OS X 10.6中模拟鼠标移动事件的方法.必须以鼠标单位(而不是像素,这很重要!)来定义它.

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.

欢迎提出任何想法.

谢谢!

推荐答案

在Mac OS X和其他操作系统中移动鼠标最简单的方法之一是使用 Java机器人.它还可以模拟其他事件.例如,按下鼠标或什至按键.但是,它将指针移动到给定的屏幕坐标.因此,您唯一需要做的就是将物理单位转换为适当的坐标.这是一个代码示例:

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);
    }
}

要测试此代码,请将其放入JavaRobotExample.java文件,然后使用以下命令对其进行编译:

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

javac JavaRobotExample.java

一旦生成JavaRobotExample.class文件,请运行它:

Once JavaRobotExample.class file is produced, run it:

java JavaRobotExample

默认情况下,Mac OS X附带Java运行时.我不确定SDK(编译器).如果您没有javac命令,只需安装 Xcode .

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.

这篇关于模拟Mac OS X中的物理鼠标移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:21
查看更多