seems like this is impossible to do! :’(
Have a look at this snippet:
public class TestConsoleKeyInputListener {
public TestConsoleKeyInputListener() {
Toolkit defaultTookKit = Toolkit.getDefaultToolkit();
defaultTookKit.addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
System.out.println("HELLO");
}
}, AWTEvent.KEY_EVENT_MASK);
AWTEventListener[] listeners = defaultTookKit.getAWTEventListeners();
System.out.println(listeners.length);
}
public static void main(String[] args) {
new TestConsoleKeyInputListener();
}
}
It returns 0! Meaning that the listener cannot be added. This is ofcourse with the “-Djava.awt.headless=true” flag on. Which is essentially what this application is, headless. But if i take that flag off, it returns 1. which is the expected value…
Even without the headless flag, it still doesn’t register key strokes like it should.
However, doing this:
public class TestConsoleKeyInputListener extends JFrame{
public TestConsoleKeyInputListener() {
Toolkit defaultTookKit = Toolkit.getDefaultToolkit();
defaultTookKit.addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
System.out.println("HELLO");
}
}, AWTEvent.KEY_EVENT_MASK);
AWTEventListener[] listeners = defaultTookKit.getAWTEventListeners();
System.out.println(listeners.length);
}
public static void main(String[] args) {
JFrame frame = new TestConsoleKeyInputListener();
frame.setSize(0, 0);
frame.pack();
frame.show();
}
}
Works fine! So i guess its time to make a “HeadlessFrame”!
DP