Listening for keypress

Can anyone explain how to use the implmentation of KeyListenter to wait for a keypress.

I'm not sure which class to register the listener to.

(I'm just learning my way around the code, modifing little bits.)


Do you mean the AWT KeyListener?

With KeyListener, do you mean java.awt.event.KeyListener? Assuming this is the case, you will have to register the KeyListener with the AWT/Swing component you want to receive the events from.

The Java Tutorial has a 'How to Write a Key Listener' chapter:


Unfortuantly not

no, this was for a command, so a keylistener needed to be attached to the commandShell. Ewout has sorted it for me.

However this does bring up a few problems that the design doesn't seem to satisfy yet. i.e. you listen to a key press, however the command shell consumes it before it gets to the command.

Yep, the shell

I should have paid attention to the fact that you posted to the Jnode-Shell forum ...

It would be nice if you could report the final solution here so that this thread is complete. It's always nice to know if problems reported to the forum have been solved (and how). Thereby the forum can also serve as a persistent reference for others that experience the same problems.



I agree.

The basic idea is to get an instance of console manager from InitalNaming.

Then from this consoleManager call getContextConsole() to get a console object that your command is running in.

Then you can register yourself as a keylistener to the console.

Remeber to de-register yourself or you would cause Null Pointer errors, in the ps2dispatch thread.

However playing with this has brought up some other questions. Will I'll post in a seperate thread.