所以我在这里有一个小问题。我正在编写一个程序,该程序在按下预定义的按钮后将变为垃圾邮件,请按下“ W”(暂时),直到再次按下该按钮为止。我正在使用JNativeHook库检查按键,但是当我的程序开始向W各处发送垃圾邮件时,该库将停止检查按下的按钮。我怎么解决这个问题?我的程序有一些片段:
public class Buttons implements NativeKeyListener {
private ButtonsAction buttonsAction = new ButtonsAction();
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_K) {
buttonsAction.startBot();
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
}
public void nativeKeyTyped(NativeKeyEvent e) {
}
public void start() {
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new Buttons());
}
}
public class ButtonsAction {
private int state = 0;
public void startBot() {
try {
Robot robot = new Robot();
while (state == 0) {
robot.keyPress(KeyEvent.VK_W);
Thread.sleep(10);
}
} catch (AWTException | InterruptedException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
问题是您正在使用同一线程来执行漫游器。您需要使用另一个线程。