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