JNativeInterface阻止JNI打印到控制台

时间:2014-10-14 01:11:43

标签: java

我正在测试网站上的JNativeInterface示例代码:

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class GlobalKeyListenerExample implements NativeKeyListener {
    public void nativeKeyPressed(NativeKeyEvent e) {
            System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

            if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
                    GlobalScreen.unregisterNativeHook();
            }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {
            System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
            System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
    }

    public static void main(String[] args) {
            try {
                    GlobalScreen.registerNativeHook();
            }
            catch (NativeHookException ex) {
                    //System.err.println("There was a problem registering the native hook.");
                    //System.err.println(ex.getMessage());

                    System.exit(1);
            }

            //Construct the example object and initialze native hook.
            GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListenerExample());
    }
}

代码本身起作用,但在鼠标移动时不断显示输出。这很令人讨厌,因为它会使控制台陷入困境,从而降低了其他更重要输出的可见性。我把它隔离到了GlobalScreen.registerNativeHook();但这似乎是对JNI的调用,所以我不确定如何阻止它显示。有什么建议吗?

0 个答案:

没有答案