我正在尝试制作一个可以检测我的击键然后基于它们做一些事情的程序,但是每次我运行我的程序时,即使我让听众初始化了任何想法,它也会以退出代码 0 结束?(我没有错误)
public class Main {
public static void main(String[] args) {
InitializeEvents initializeEvents = new InitializeEvents();
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
e.printStackTrace();
}
GlobalScreen.getInstance().addNativeKeyListener(initializeEvents.getKeyboard());
GlobalScreen.getInstance().addNativeMouseListener(initializeEvents.getMouse());
}
}
这是我初始化事件的地方
public class InitializeEvents {
private final NativeKeyboardEvent keyboard;
private final NativeMouseClickEvent mouse;
public InitializeEvents() {
keyboard = new NativeKeyboardEvent();
mouse = new NativeMouseClickEvent();
}
public NativeKeyboardEvent getKeyboard() {
return keyboard;
}
public NativeMouseClickEvent getMouse() {
return mouse;
}
}
最后这是我的活动
public class NativeKeyboardEvent implements NativeKeyListener {
@Override
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("test");
}
@Override
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("test");
}
@Override
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("test");
}
}
关于它的奇怪之处在于它工作得更早,但是当我单击一个键时它随机停止打印“测试”,即使没有任何改变。