如果我的理解对我有用,可以使用以下两个命令打开和关闭正确的 jnativehook:
GlobalScreen.removeNativeKeyListener(this); //这将其关闭
GlobalScreen.addNativeKeyListener(this);//这会再次打开它
按照这个逻辑,下面的程序:
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());
}
}
据我了解,它应该打印这样的东西。
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
相反,我得到了这个:
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
发生的事情是,即使我使用了命令,nativehook 也会不断重新注册所有输入
GlobalScreen.removeNativeKeyListener(this);
当我用run2()将它添加回来时,程序会遍历我在输入时给它的所有按键(以及它们调用的方法,在这种情况下是按下“a”时的run())。为什么会有这种行为?