Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How to get instant key input in Java?
I want to make a program which takes in key input and makes something happen. How can I make the program so that pressing Ctrl - R does something?
I don't want to use any buttons. Just like I said in the question, I wish to press Ctrl - R to make something happen. Anything. I'll make that part, it's not relevant. How can I code the part where I the program will actually take my key input?
5 Answers
- ?Lv 59 years agoFavorite Answer
As has been mentioned before, you can (and should) use the keyListener interface.
You can add the listener to any graphical window BUT that won't work for the console as there's no way to hook the listener.
The console input, System.In, by design, is an InputStream of characters, abstracted away from any underlying hardware. You can use a Scanner (which has some fancy methods like nextInt() ) to read the InputStream, or you can use the input's own methods: available() tell you how many bytes are lying in the input stream, and read() gives you one (waiting if necessary).
The problem with the second approach is the layer of abstraction. Often the console assumes it is going to be read by lines, and does not send any data to the system input until the Enter key is pressed, so the user can edit the last line of input. At this moment, the internal line buffer is fed into the input stream and discarded.
So, you can capture individual key events for a GUI window (JFrame.addKeyListener() ), but you can't reliably capture individual key events for the system console since the user might be able to "un-press" them using the backspace key or edit the input in the middle.
- ?Lv 45 years ago
My perfect chum's mom works at an Alltel keep and says that the Razor V3 has had the main problems and return share. She recommends the Motorola E815 (or larger) telephone that would keep %., take photos and checklist video besides as an MP3 participant. no longer something yet undesirable from those Razors....looks sturdy, yet malfunctions each and every of the time she says. word to the smart...do no longer purchase one!!!
- modulo_functionLv 79 years ago
- 9 years ago
It cant be done by native Java alone ..
You'll need to use platform specific library.
- How do you think about the answers? You can sign in to vote the answer.
- 9 years ago
show me what you got and we can work on it. any button can do something, not sure what you would want it to do, but show some code.