Code:
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());
}
}
这是给定的"的代码, http://code.google.com/p/jnativehook/wiki/examples" rel="nofollow"。 我下载了这个代码, 然后在我的项目中使用了 http://code.google.com/p/jnativehook/ downloads/list" rel="no follow" >JativeHost 库 。 但我得到以下错误 :
Cannot find unregisterNativeHook,registerNativeHook. //(line 18,38)
The IDE also says GlobalKeyListenerExample is not abstract and doesn t override abstract method keyReleased.
第一个错误,我还导入了GlobScreen类。
为什么我得到第二个错误? “ 强” 但当我在使用该方法之前添加一个 Override
注释时, IDE 给出了一个错误, 表示该方法不会从超级类型上取代或执行 。 强”