我正在使用 JNativeHook 进行全局键盘监听,但在 Windows 10 上从未触发 nativeKeyTyped 方法。始终触发 nativeKeyPressed 和 nativeKeyReleased 方法。所有方法都在 linux 上运行良好。有人可以帮我解决这个问题吗?
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class KeyLogger implements NativeKeyListener {
private static final Path file = Paths.get("keys.txt");
private static final Logger logger = LoggerFactory.getLogger(KeyLogger.class);
public static void main(String[] args) {
logger.info("Key logger has been started");
init();
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
logger.error(e.getMessage(), e);
System.exit(-1);
}
GlobalScreen.addNativeKeyListener(new KeyLogger());
}
private static void init() {
// Get the logger for "org.jnativehook" and set the level to warning.
java.util.logging.Logger logger = java.util.logging.Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.WARNING);
// Don't forget to disable the parent handlers.
logger.setUseParentHandlers(false);
}
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("nativeKeyPressed -> getKeyCode " + e.getKeyCode());
System.out.println("nativeKeyPressed -> getKeyChar " + e.getKeyChar());
System.out.println("nativeKeyPressed -> getRawCode " + e.getRawCode());
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("nativeKeyReleased -> getKeyCode " + e.getKeyCode());
System.out.println("nativeKeyReleased -> getKeyChar " + e.getKeyChar());
System.out.println("nativeKeyReleased -> getRawCode " + e.getRawCode());
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("nativeKeyTyped -> getKeyCode " + e.getKeyCode());
System.out.println("nativeKeyTyped -> getKeyChar " + e.getKeyChar());
System.out.println("nativeKeyTyped -> getRawCode " + e.getRawCode());
}
}