我看了一些例子,並試圖在Google上搜索幾個小時,但似乎我找不到一個涵蓋我所問的問題。用JNativeHook註冊多鍵壓力機
如果我想這樣做
W +一個在同一時間,並註冊爲一個獨立的按鍵事件而不是僅僅作爲W和單獨的,我怎麼會去這樣做使用JNativeHook是什麼?是否因爲我錯過了某個覆蓋此類的課程,或者是否有某種解決方法只能讀取一個密鑰?
我想在控制檯中做到這一點(因此我使用這個庫,而不是Swing)。
我把他們的榜樣,並希望嘗試和修改:
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class GlobalKeyListener implements NativeKeyListener {
@Override
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
GlobalScreen.unregisterNativeHook();
}
}
@Override
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
@Override
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
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);
}
//Construct the example object and initialze native hook.
GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListener());
}
}