代码 :
package keylogger;
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.VK_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook(); // LINE 18
}
catch (NativeHookException ex) {
System.err.println("You cannot call unregisterNativeHook() from the native dispatch thread.");
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook(); // LINE 38
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
ex.printStackTrace();
}
//Construct the example object and initialze native hook.
GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListenerExample());
}
}
这与google code 上给出的代码相同。我下载并在我的项目中使用了JNativeHost 库。但我收到以下错误:
Cannot find unregisterNativeHook,registerNativeHook. //(line 18,38)
The IDE also says GlobalKeyListenerExample is not abstract and doesn't override abstract method keyReleased.
对于第一个错误,方法是在此处定义的,它们是本机方法,我还导入了 GlobalScreen 类。
为什么我已经覆盖了第二个错误?但是,当我@Override
在该方法之前添加注释时,IDE 会给出一个错误,指出该方法不会覆盖或从超类型实现。