Read from stdin

Alain Magloire magloire at cae.ca
Sun Mar 15 13:10:17 PST 1998


Bonjour

> I know this is not related only to kaffe but maybe some of you can help
> me.
> 

:-), yes see Michael R. Harper posting.
Or try the java-linux lists on blackdown.
Since kaffe is not a "heavy" list, I don't think
people would mind, unless it is spam. 

> I am trying to read only one character from the keyboard (i am using kaffe
> 0.9.2 on text "console" mode) without hiting return.
> 
> I can type many characters but they get read only when i hit return.
> 

hmm .. I think this is to platform dependent for Java. Unfortunately Java 1.x
does not come with a rich set of classes to manipulate Terminals (ttys).
At one point, I've seen message about some sort of curses like package ...
but who use line mode nowadays ;-)

> What i am looking for is something like the C function getche (on Turbo C
> or Borland C).
> 

On unix, we don't have such beast, since the terminals have there own
line discipline. You would have to link with one of curses (BSD) or
ncurses (SVR4) librairies.

You could roll up your "portable" C function and load it with JNI.
Or wrap the program with a call to :
	stty -echo -icanon
and at the end of the script call :
	stty echo icanon

> Thanks in advance.
> 

The obvious way with the AWT 1.1 event model goes something like this :

import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class key
{
    public static void main (String [] args)
    {
        Frame f = new Frame ();
        f.setSize (200, 300);
        f.setTitle ("Coucou");
        f.addKeyListener ( new KeyAdapter () {
            public  void keyPressed (KeyEvent evt)
            {
                System.out.println ("Key : " + evt.getKeyChar ());
            }
        });
        f.setVisible (true);
    }
}

See Michael R. Harper for pointers to docs.

--
alain



More information about the kaffe mailing list