我尝试使用 awt.Robot 以编程方式创建鼠标和按键事件。问题是关键事件没有得到很好的处理。没有错误信息。这是代码:
package com.ClCH38.CC_Macro.TDI;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
public class Tdi {
Robot robot;
Tdi tdi;
public Tdi() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public void directInputKey() {
try {
robot.keyPress(KeyEvent.VK_1); // Choose slot 1. Work fine
Thread.sleep(50);
robot.keyRelease(KeyEvent.VK_1);// Needed to release key
Thread.sleep(50);
robot.keyPress(KeyEvent.VK_2); // Choose slot 2.@
The item is put in hand, but slot 1 is seems always selected
Thread.sleep(50);
robot.keyRelease(KeyEvent.VK_2);// Needed to release key
Thread.sleep(50);
robot.keyPress(KeyEvent.VK_1); // Choose slot 1 Don’t change anything
Thread.sleep(50);
robot.keyRelease(KeyEvent.VK_1);// Needed to release key
Thread.sleep(50);
robot.keyPress(KeyEvent.VK_S); // Move player back Don’t work
Thread.sleep(50);
robot.keyRelease(KeyEvent.VK_S);// Needed to release key
Thread.sleep(50);
robot.mousePress(MouseEvent.BUTTON3_DOWN_MASK);// Place block
Thread.sleep(50);
robot.mouseRelease(MouseEvent.BUTTON3_DOWN_MASK); // mouse button 3 is for AWT mouse right button
Thread.sleep(50);
robot.mousePress(MouseEvent.BUTTON3_DOWN_MASK);// Don’t work
Thread.sleep(50);
robot.mouseRelease(MouseEvent.BUTTON3_DOWN_MASK);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
我在 Mac OS 10.10.9、Java 8 上工作