| | |
Detect KeyPress From Console?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
Short version of my question:
__________________________________________
How can I detect a keypress in Java without using a GUI?
Long Version:
__________________________________________
I am writing a program that users System.out and System.in for its input and output. As you are no doubt familiar, if you are in the middle of typing something and the program sends something to System.out, it will just put it right on top of where you were typing and move you to the next line and it all just looks like a mess.
Say you are typing "this is a line" and after you type the second "i" but before you type the "n", the program returns "OUTPUT\n". Your screen looks like this:
To make things a little cleaner, you can put a "\r" at the beginning of each line of output so that output will always be placed at the start of the line... but then you still have to just remember what you typed before that. Here's the same situation, only the program outputs "\rOUTPUT\n":
I am trying to clean this up a bit and make my program more user friendly. I want to set it up so that if you are in the middle of typing something and the program returns output, everything you have typed so far gets moved down one line and the output appears on the line you were just typing on.
So the same example would look like this:
I hope that description makes sense.
To do that, I have developed a (very overly complicated) method to implement this... but it relies on me being able to do one very important thing that I have not yet figured out how to do: I need to be notified of each keypress right after it happens rather than waiting for the user to press enter and send an entire line of text.
I have tried wrapping System.in to a BufferedInputStream with a size of 1... no effect.
I have tried using an invisible JFrame to implement KeyListener, but apparently elements can only get KeyEvents if they are both visible and in focus.
My last resort is to try to set the console into cbreak mode... which means each character becomes available as soon as it is typed. But I can't figure out how to do that under Windows. The Java program will start from a batch file, so I can issue any Windows command line commands that I need to before running the program... I could also run a .exe file or something like that. But so far I have not found anything that will allow me to put the Windows terminal into single character buffer mode instead of line buffer mode.
These are just some ideas of the things I have tried in case they give anyone an idea. All I want to do is detect keypresses in Java from the console without launching a visible GUI.
Can anyone think of a way to do this?
__________________________________________
How can I detect a keypress in Java without using a GUI?
Long Version:
__________________________________________
I am writing a program that users System.out and System.in for its input and output. As you are no doubt familiar, if you are in the middle of typing something and the program sends something to System.out, it will just put it right on top of where you were typing and move you to the next line and it all just looks like a mess.
Say you are typing "this is a line" and after you type the second "i" but before you type the "n", the program returns "OUTPUT\n". Your screen looks like this:
Java Syntax (Toggle Plain Text)
this is a liOUTPUT ne
Java Syntax (Toggle Plain Text)
OUTPUT ne
So the same example would look like this:
Java Syntax (Toggle Plain Text)
OUTPUT this is a line
I hope that description makes sense.
To do that, I have developed a (very overly complicated) method to implement this... but it relies on me being able to do one very important thing that I have not yet figured out how to do: I need to be notified of each keypress right after it happens rather than waiting for the user to press enter and send an entire line of text.
I have tried wrapping System.in to a BufferedInputStream with a size of 1... no effect.
I have tried using an invisible JFrame to implement KeyListener, but apparently elements can only get KeyEvents if they are both visible and in focus.
My last resort is to try to set the console into cbreak mode... which means each character becomes available as soon as it is typed. But I can't figure out how to do that under Windows. The Java program will start from a batch file, so I can issue any Windows command line commands that I need to before running the program... I could also run a .exe file or something like that. But so far I have not found anything that will allow me to put the Windows terminal into single character buffer mode instead of line buffer mode.
These are just some ideas of the things I have tried in case they give anyone an idea. All I want to do is detect keypresses in Java from the console without launching a visible GUI.
Can anyone think of a way to do this?
>> As you are no doubt familiar, if you are in the middle of typing something and the program sends something to System.out, it will just put it right on top of where you were typing
Usually "you" an "program" are one and the same. Program doesn't send anything by itself.
The way I see it, all you have to do is remove/comment-out the part where your program does System.out() ! Or instead of writing to System.out make it write to some file instead.
I fails to understand the usecase where one would want a program to accept input from user while at the same time writing some output to the screen..
Do you have multiple threads? Can you post your code.. ?
Usually "you" an "program" are one and the same. Program doesn't send anything by itself.
The way I see it, all you have to do is remove/comment-out the part where your program does System.out() ! Or instead of writing to System.out make it write to some file instead.
I fails to understand the usecase where one would want a program to accept input from user while at the same time writing some output to the screen..
Do you have multiple threads? Can you post your code.. ?
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
I can post my code if you really want to see it, but I don't think it will really help much.
The program is a chat program, if that helps you understand why I need this functionality. It's actually more than that, but it has a chat feature to it. The main thread is the part you are using, where you type commands and the program responds instantly. But there is also a thread listening for incoming chat messages, and those get displayed as soon as they arrive, so one might arrive while you are in the middle of typing something.
The program is a chat program, if that helps you understand why I need this functionality. It's actually more than that, but it has a chat feature to it. The main thread is the part you are using, where you type commands and the program responds instantly. But there is also a thread listening for incoming chat messages, and those get displayed as soon as they arrive, so one might arrive while you are in the middle of typing something.
![]() |
Similar Threads
- keypress detection (C)
- Need Help In C++ (C++)
- ADMIN$ not found when installing Symantec 8.1 from Console (Networking Hardware Configuration)
Other Threads in the Java Forum
- Previous Thread: How to call a EJB method from Session bean method
- Next Thread: Need Help Quick!
Views: 6012 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database detection draw eclipse error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle os physics print problem program programming project radio recursion remote scanner screen security server set size sms socket sort sql string swing test textfield threads time transfer tree trolltech utility windows





