1

我正在使用 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());
    }
}
4

1 回答 1

0

它工作得很好。这是我运行程序并输入“p”时的输出

nativeKeyPressed -> getKeyCode 25
nativeKeyPressed -> getKeyChar 
nativeKeyPressed -> getRawCode 35
nativeKeyTyped -> getKeyCode 0
nativeKeyTyped -> getKeyChar p
nativeKeyTyped -> getRawCode 35
nativeKeyReleased -> getKeyCode 25
nativeKeyReleased -> getKeyChar 
nativeKeyReleased -> getRawCode 35

请记住

Key down and up events produce "codes" and key typed events produce "chars." 
Key down/up events will always have a char of CHAR_UNDEFINED and key typed 
events will always have a code of VK_UNDEFINED. This should match the behaviour 
of Java's KeyEvent.
于 2019-03-08T09:28:25.293 回答