Console KeyListener

Hi all,
I was wondering if there is some Reader/Buffer combination that gives the same result as a KeyListener. Ie, when a key is being pressed.

Basically, i have this command line application that is waiting for the user to input a command ( a play button from a video recorder ), but a video recorder doesn’t expect you to press enter afterwards :slight_smile:

So say after the user press ‘p’, the reader would return…

DP

I’m not sure if theres a buffer/reader combo but have you tried adding a KeyListener to the AWT event queue. It might still pick up events even without the window?

Kev

Sorry for being very naive here, but how would I accomplish this?

I know JFrames have a .setKeyListener(KeyListener) method. You said something about the AWT event queue, how would i be able to do that?

I googled for some stuff, but they all use JFrame/Canvas…etc

Thank you, DP

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#addAWTEventListener(java.awt.event.AWTEventListener,%20long)

Should get you every event that comes through awt. Mask out for keyboard events. I’m not 100% sure whether you might have to initialise AWT (create a button or something) before trying to use it…

Let me know, I have some console level stuff coming up that I’d like to use it… hmm… console based 4k game! :wink:

Kev

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

What happens if you don’t show or pack the frame?

Kev

nope, it needs a Component for the AWT event queue to start up. And its just too much hassle. And since i am developing on a pure linux arch, im going to be using this:


FileInputReader reader = null;

try {
  reader = new FileInputReader("/dev/tty0");
}catch (FileNotFoundException fnfe) {
  // file not found
}

int nChar = 0;
try {
  nChar = reader.read();
}catch (IOException ioe) {
  // couldn't read
}

Thats the work around…

DP