我一直在试用jnativehook库和提供的示例,但我仍然无法触发关键方法发布。
package hookexample;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class NativeHookDemo extends JFrame implements NativeKeyListener, WindowListener {
public NativeHookDemo() {
// Set the event dispatcher to a swing safe executor service.
GlobalScreen.getInstance().setEventDispatcher(new SwingExecutorService());
setTitle("JNativeHook Swing Example");
setSize(300, 150);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(this);
setVisible(true);
}
public void windowOpened(WindowEvent e) {
//Initialze native hook.
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
ex.printStackTrace();
System.exit(1);
}
GlobalScreen.getInstance().addNativeKeyListener(this);
}
public void windowClosed(WindowEvent e) {
//Clean up the native hook.
GlobalScreen.unregisterNativeHook();
System.runFinalization();
System.exit(0);
}
public void windowClosing(WindowEvent e) { /* Unimplemented */ }
public void windowIconified(WindowEvent e) { /* Unimplemented */ }
public void windowDeiconified(WindowEvent e) { /* Unimplemented */ }
public void windowActivated(WindowEvent e) { /* Unimplemented */ }
public void windowDeactivated(WindowEvent e) { /* Unimplemented */ }
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key released!");
if (e.getKeyCode() == NativeKeyEvent.VC_SPACE) {
JOptionPane.showMessageDialog(null, "This will run on Swing's Event Dispatch Thread.");
}
}
public void nativeKeyPressed(NativeKeyEvent e) { /* Unimplemented */ }
public void nativeKeyTyped(NativeKeyEvent e) { /* Unimplemented */ }
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingExample();
}
});
}
private class SwingExecutorService extends AbstractExecutorService {
private EventQueue queue;
public SwingExecutorService() {
queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
}
public void shutdown() {
queue = null;
}
public List<Runnable> shutdownNow() {
return new ArrayList<Runnable>(0);
}
public boolean isShutdown() {
return queue == null;
}
public boolean isTerminated() {
return queue == null;
}
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return true;
}
public void execute(Runnable r) {
EventQueue.invokeLater(r);
}
}
}
我只是简单地复制了https://github.com/kwhat/jnativehook/wiki/Usage上的示例,将类名和包名一起更改,并在nativeKeyReleased()中的console命令中插入了一个System print。添加了库(JNativeHook v1.2.0-RC3)。当我运行代码时,它确实在控制台中显示鼠标和键事件的日志,但nativeKeyReleased()从不触发。我错过了什么吗?