Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2005

Tip: Looking for answers? Try searching our database.

Immediately reading characters from System.in

Thread view: 
frankgerlach22@gmx.de - 20 Aug 2005 17:33 GMT
I am creating a shell program in java. For this purpose, I would like
to *immediately* read every key pressing. Eg. I would like to read
'\t', immediately, not after '\n' has been pressed. It seems that the
InputStream buffers all input until '\n' is pressed. Can I modify this
behaviour ?
Gordon Beaton - 20 Aug 2005 18:37 GMT
> I am creating a shell program in java. For this purpose, I would like
> to *immediately* read every key pressing. Eg. I would like to read
> '\t', immediately, not after '\n' has been pressed. It seems that the
> InputStream buffers all input until '\n' is pressed. Can I modify this
> behaviour ?

Console buffering is done by the console, independent of Java. If you
can modify the console mode, then yes. On platforms where stty is
available, you can use something like this:

 static String getInputMode() {
   String mode = null;

   try {
     String[] cmd = {
       "/bin/sh",
       "-c",
       "/bin/stty -g < /dev/tty"
     };
     Process p = Runtime.getRuntime().exec(cmd);
     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
     mode = br.readLine();
     p.waitFor();
     br.close();
   }
   catch (IOException e) {} // should probably deal with this
   catch (InterruptedException e) {}

   return mode;
 }

 static void setInputMode(String mode) {
   try {
     String[] cmd = {
       "/bin/sh",
       "-c",
       "/bin/stty " + mode + " < /dev/tty"
     };
     Process p = Runtime.getRuntime().exec(cmd);
     p.waitFor();
   }
   catch (IOException e) {}
   catch (InterruptedException e) {}
 }

Then:

 String savedMode = getInputMode();
 
 try {
   setInputMode("-icanon min 1");
   doRestOfProgram();
 }
 finally {
   setInputMode(savedMode);
 }

Note that by doing this you lose the command line editing capabilities
normally provided by the console.

/gordon  

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Raymond DeCampo - 20 Aug 2005 21:40 GMT
> I am creating a shell program in java. For this purpose, I would like
> to *immediately* read every key pressing. Eg. I would like to read
> '\t', immediately, not after '\n' has been pressed. It seems that the
> InputStream buffers all input until '\n' is pressed. Can I modify this
> behaviour ?

If you want to stay platform-independent, I recommend using Swing or AWT
to display a text area for input and output.  Then you will have full
control.

HTH,
Ray

Signature

XML is the programmer's duct tape.

Roedy Green - 21 Aug 2005 04:02 GMT
>If you want to stay platform-independent, I recommend using Swing or AWT
>to display a text area for input and output.  Then you will have full
>control.

see http://mindprod.com/jgloss/console.html
and http://mindprod.com/jgloss/easyin.html

The equivalent of getC does not exist in Java.
Joan - 21 Aug 2005 07:29 GMT
>>If you want to stay platform-independent, I recommend using
>>Swing or AWT
[quoted text clipped - 6 lines]
>
> The equivalent of getC does not exist in Java.

So JNI is the answer.
Roedy Green - 22 Aug 2005 18:34 GMT
>So JNI is the answer.

that's one way if your platform has a getc.

The other is to fake something with AWT keystroke events.

These you do get as they happen.

The other half of the problem is echoing chars back to the user so
they can see what they typed, and providing some primitive sort of
editing so they can back up when typing strings.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.