English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am working on a Java application that gets input and sends output to the system console (System.out and System.in). I need to detect the pressing of a certain key right when it is pressed... not once the user presses enter.

For example, say I need to detect the pressing of the 'A' key, and when that happens, the program will do System.out.println("YOU PRESSED THE A KEY");

If a user tries to type the word "roman" this will happen:

romaYOU PRESSED THE A KEY
n

I hope that makes sense. I know there is no method for doing this, but any work around would do, including creating an invisible component to implement a KeyListener (though from what I have seen, components can only listen for keys if they are visible). Also, I need to do this using pure Java, not system-specific libraries.

Thanks very much to anyone who can help!

2007-03-28 18:44:07 · 3 answers · asked by Stephen W 1 in Computers & Internet Programming & Design

>You need to output a terminal control sequence
>to switch to cbreak mode instead of the default
>line-buffered mode. Then you can read
>characters immediately instead of entire lines.

Thanks very much to the speedy reply, but can you be a little more specific about how to do this?

2007-03-28 19:14:34 · update #1

>I think this is what you are looking for
>Source(s):
>http://www.particle.kth.se/~fmi/kurs/phy...
Sorry, that doesn't work. It still waits for the user to press enter rather than reading in each character as it is typed.

2007-03-28 19:55:31 · update #2

>simple catch the keypressed event
>Source(s):
>Find Computer Tutorials, Software
>Books,Interview Question And Answer at
>http://myitcareer.org/

As I said before, only componenets can implement KeyListener, and then they can only catch KeyEvents if they are visible and in focus. Did you even read my question?

2007-03-29 04:49:12 · update #3

3 answers

You need to output a terminal control sequence to switch to cbreak mode instead of the default line-buffered mode. Then you can read characters immediately instead of entire lines.

2007-03-28 19:02:56 · answer #1 · answered by undercoloteal 3 · 0 0

simple catch the keypressed event

2007-03-29 01:02:51 · answer #2 · answered by Anonymous · 0 0

I think this is what you are looking for

2007-03-28 19:49:02 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers