2

first time making a question.

What I want is a way of every time the user presses a key on the console certains actions take place. Like, as he types a word, I want at every keypress for a String formed by all the keys he's already pressed to be printed, concatenated to the newly pressed key. As in:

a

You typed: a

b

You typed: ab

c

You typed: abc

d

You typed: abcd

e

You typed: abcde

I'm trying to do this with the following code:

try (BufferedReader input = new BufferedReader(
                            new InputStreamReader(System.in, "UTF-8"))) {
    char c = 0;
    String s = "";
    while( (c = (char) input.read() ) != 13) {
        s += c;
        System.out.println("You typed: " + s);
    }
}

I get what I want, but just after I press the Enter key, not as every key pressed on the console:

foobar

You typed: f

You typed: fo

You typed: foo

You typed: foob

You typed: fooba

You typed: foobar

Thanks in advance.

rcf
  • 58
  • 2
  • 8
  • http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it Give this link a look – Josh Engelsma Jan 12 '14 at 00:49

1 Answers1

3

It looks like someone else has asked this question. It also looks like that consensus is that modifying System.in is different across platforms and in order to do what you want, you would need to change the terminal from "line" mode to "character" mode.

Take a look Here.

Community
  • 1
  • 1
ehaydenr
  • 240
  • 2
  • 12