If my understanding serves me correct jnativehook can be turned on and off with these two commands:
GlobalScreen.removeNativeKeyListener(this); //this shuts it down
GlobalScreen.addNativeKeyListener(this);//and this turns it back on again
by this logic the program below:
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class GlobalKeyListenerExample implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
String s = NativeKeyEvent.getKeyText(e.getKeyCode());
handle(s);
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
logger.setUseParentHandlers(false);
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException ex) {
ex.printStackTrace();
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void handle(String k) {
if (k == "A") {//close nativeKeylistener and execute run()
GlobalScreen.removeNativeKeyListener(this);
run();
run2();//turn keylistener back on
}
}
public void run() {
System.out.println("Hello there!");
System.out.print("Write something, please: ");
Scanner lukija = new Scanner(System.in);
String str = lukija.nextLine();
System.out.println("you wrote: "+str);
}
public void run2() {
GlobalScreen.addNativeKeyListener(this);
}
public static void main(String[] args) {
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
logger.setUseParentHandlers(false);
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);
}
GlobalScreen.addNativeKeyListener(new GlobalKeyListenerExample());
}
}
It should print something like this, by my understanding.
kKey Pressed: K
Key Pressed: J
jKey Pressed: G
gKey Pressed: U
uKey Pressed: Y
yKey Pressed: T
tKey Pressed: H
hKey Pressed: V
vKey Pressed: B
bKey Pressed: N
nKey Pressed: H
hKey Pressed: G
gKey Pressed: A
Hello there!
Write: i am a robot
i am a robot
Instead i get this:
kKey Pressed: K
Key Pressed: J
jKey Pressed: G
gKey Pressed: U
uKey Pressed: Y
yKey Pressed: T
tKey Pressed: H
hKey Pressed: V
vKey Pressed: B
bKey Pressed: N
nKey Pressed: H
hKey Pressed: G
gKey Pressed: A
Hello there!
Write: i am a robot
i am a robot
Key Pressed: Backspace
Key Pressed: I
Key Pressed: Space
Key Pressed: A
Hello there!
Write: test
test
Key Pressed: M
Key Pressed: Space
Key Pressed: A
Hello there!
Write: test
test
Key Pressed: Space
Key Pressed: R
Key Pressed: O
Key Pressed: B
Key Pressed: O
Key Pressed: T
Key Pressed: Enter
Key Pressed: T
Key Pressed: E
Key Pressed: S
Key Pressed: T
Key Pressed: Enter
Key Pressed: T
Key Pressed: E
Key Pressed: S
Key Pressed: T
What happens is that nativehook keeps rergistering all the inputs even tho i have used the command
GlobalScreen.removeNativeKeyListener(this);
and when i add it back it with run2(), the program goes through all the key presses i gave it while giving input(and the methods they invoke, in this case run() when "a" is pressed). Why this behavior?