Discussion:
Keylistener without GUI?
(too old to reply)
Ivan Bout
2004-12-11 12:52:28 UTC
Permalink
Hi!

Does anyone know a way how to capture keyboard events from a
console program without using java.awt.events?

I tried:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
s = br.readLine();

But the problem is that I have to press the enter key each time.


Thanks in advance

Ivan
Chris Smith
2004-12-11 15:47:53 UTC
Permalink
Post by Ivan Bout
Does anyone know a way how to capture keyboard events from a
console program without using java.awt.events?
There is no way. At that point, key events are delivered by the
operating system to the console window that's running your application.
It's up to that console window to decide when to communicate with your
application via System.in. At best, you can find out when a key is
typed; but the console window is going to swallow the exact times that a
key is pressed or released, so you're out of luck there.
Post by Ivan Bout
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
s = br.readLine();
But the problem is that I have to press the enter key each time.
There are two potential reasons for that. One is that you're calling
readLine, and that method waits for a newline by definition. The other,
though, is that console apps sometimes (depending on the operating
system) will actually wait until ENTER is pressed before they will send
you any keyboard input. How you'd change this, and whether you can
change this, is going to depend on the operating system, and perhaps the
terminal program and/or implementation of the JVM. Basically, you
should not assume that what you want to do is possible.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Ivan Bout
2004-12-11 17:02:33 UTC
Permalink
Post by Chris Smith
Post by Ivan Bout
Does anyone know a way how to capture keyboard events from a
console program without using java.awt.events?
There is no way. At that point, key events are delivered by the
operating system to the console window that's running your application.
It's up to that console window to decide when to communicate with your
application via System.in. At best, you can find out when a key is
typed; but the console window is going to swallow the exact times that a
key is pressed or released, so you're out of luck there.
Post by Ivan Bout
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
s = br.readLine();
But the problem is that I have to press the enter key each time.
There are two potential reasons for that. One is that you're calling
readLine, and that method waits for a newline by definition. The other,
though, is that console apps sometimes (depending on the operating
system) will actually wait until ENTER is pressed before they will send
you any keyboard input. How you'd change this, and whether you can
change this, is going to depend on the operating system, and perhaps the
terminal program and/or implementation of the JVM. Basically, you
should not assume that what you want to do is possible.
Thanks for your reply! After surfing some websites I found out that the
problem lies in the tty driver for linux.

After changing the code to:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i = br.read();
char c = (char) i;

And run this command before running my java program:

stty -icanon

It just works fine without pressing ENTER!

Loading...