Reputation: 2678
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());
}
}
This is same code as given here on google code. I downloaded and then used the JNativeHost library in my project. But i get the following errors :
Cannot find unregisterNativeHook,registerNativeHook. //(line 18,38)
The IDE also says GlobalKeyListenerExample is not abstract and doesn't override abstract method keyReleased.
To the first error the methods are defined here and they are native methods and i also have imported the GlobalScreen class.
And why do i get the second error when i have already overrided that ? But when i add a @Override
annotation before that method IDE gives an error saying the method doesn't override or implement from a supertype.
Upvotes: 3
Views: 2781
Reputation: 310840
If you are using JDK 1.5, @Override can only be used when overriding methods from base classes, not when implementing methods from interfaces.
Upvotes: 0
Reputation: 23206
Everything is fine ! One thing that i think could be wrong is the version of the library you might be using against the code you have copied and pasted ! Use the marked library below. You can download it from here
From the comments i guess you are using netbeans. Add this jar file to your library.
Upvotes: 2
Reputation: 14149
If you are using eclipse, try starting with an empty class like this:
public class GlobalKeyListenerExample implements NativeKeyListener {
}
You should get an error on the declaration and a quick fix 'implement abstract methods' should be available. Execute the quick fix and see whether it compiles afterwards (normally it should). You can then go on and add your logic.
Upvotes: 2
Reputation: 7234
Most possibly the method which you say you have overridden has a different signature than that exists in the base class. So, your method is not considered to be overridden but is a new overloaded version, altogether. Verify that the method signature is correct.
Upvotes: 0