Reading keystrokes using Java under Linux terminals?

I

Ivan_G_S

hello, community!

i am trying to read single characters and keystrokes with a java
console application using linux terminals (konsole, xterm).

int inputChar = 0;
while ((inputChar = System.in.read()) != -1) {
char input = (char) inputChar;
System.out.print(" <" + input + "> ");
System.out.flush();
}

but instead of getting one keystroke after another, i get them all at
once after pressing return.
furthermore, control characters, instead of being processed, are
displayed this way
^[[A^[[B^[[D^[[C^[
after pressing return, only [ appears.

( arrows: up, down, left, right; esc. )

is there a possibility to do this correctly at all using java?
or do i have to write a GUI application to handle keystrokes?

thanks in advice!
 
J

Joshua Cranmer

Ivan_G_S said:
hello, community!

i am trying to read single characters and keystrokes with a java
console application using linux terminals (konsole, xterm).

int inputChar = 0;
while ((inputChar = System.in.read()) != -1) {
char input = (char) inputChar;
System.out.print(" <" + input + "> ");
System.out.flush();
}

but instead of getting one keystroke after another, i get them all at
once after pressing return.

Because that's how consoles work, most of the time. The console has a
line buffer, and <Enter> commits the buffer. If you don't want this,
you'll want to use something called curses. Google gets
<http://sourceforge.net/projects/javacurses/>, but I have no experience
with that whatsoever. It appears to be something designed more for the
GUI side and styled similar to the AWT, though; it should still allow
you to get characters correctly, though.
^[[A^[[B^[[D^[[C^[
after pressing return, only [ appears.

Is that what's being printed by the application or not? Use integral
output (i.e. (int) input or just inputChar if you prefer) to see what's
really going on.

If it's not what's being printed, welcome to the wonderful world of ANSI
terminal escape codes. You have moved the cursor first up a row, than
down a row, left a column, and finally right a column. See
<http://en.wikipedia.org/wiki/ANSI_escape_code> for more information.
 
G

Gordon Beaton

i am trying to read single characters and keystrokes with a java
console application using linux terminals (konsole, xterm).

int inputChar = 0;
while ((inputChar = System.in.read()) != -1) {
char input = (char) inputChar;
System.out.print(" <" + input + "> ");
System.out.flush();
}

but instead of getting one keystroke after another, i get them all at
once after pressing return.

As others have already pointed out, it's the terminal, not Java that
is buffering the keystrokes. It is possible to get character-at-a-time
behaviour but you'll lose the primitive editing capabilities provided
by the terminal.

For an example, see
http://groups.google.com/group/comp.lang.java.programmer/msg/df697a1d029dd808
(or http://tinyurl.com/3v2frx)

/gordon

--
 
D

David Zimmerman

Gordon said:
As others have already pointed out, it's the terminal, not Java that
is buffering the keystrokes. It is possible to get character-at-a-time
behaviour but you'll lose the primitive editing capabilities provided
by the terminal.

try issuing on the command line before your program
stty raw

this will disable the buffering
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top