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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With