Results 1 to 9 of 9

Thread: True single input on command line

  1. #1

    Thread Starter
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33

    True single input on command line

    Can anyone actually tell me a real platform independent way of esentially do what QBasic would have been an inkey$ (oh for the days of QBasic), I just want to grab the next key pressed, or ideally whatever key is being pressed at this precise moment in time.......

    Someone suggested this:
    Code:
    String tmp = "";
      char C='\0';
      try{
        while ((C=(char) System.in.read()) !='\n')
        {
          if (C != '\r')  tmp = tmp+C;
          System.out.print('\r');
        }
      }  catch(IOException err){}
    And that this would use System.in.read() to input single characters until return ('\r' within unicode) is entered.........

    but it doesn't

    I tried to go through the java source within System, and the classes for in (can't remember the class now), etc, but to no avail...

    Any suggestions?

    Dave.

    PS if it looks stupid it's opera playing me up



    Infinity isn't large it's just incomprehensible
    Last edited by daveyboy; Mar 20th, 2002 at 01:33 PM.

  2. #2

    Thread Starter
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    hmmmm, also any ideas why my code section buggered up like that?













    *** k I sussed this!!! ******





    Infinity isn't large, it's just incomprehensible

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    try this on for size.
    Code:
     
      import java.io.*;
    
      public class Read {
       public static void main(String[] args){
        
       InputStreamReader isr = new InputStreamReader(System.in);
       StringBuffer sb = new StringBuffer(); 
    
      try{ 
       while(true){
        char c = (char) isr.read();
         sb.append(c);         
         if(c =='\r'){
          System.out.println("Enter was pressed!");
            System.out.println(" Keys that were pressed so far: " + sb);
            break;
         } 
        }
       }catch(IOException e){System.err.println(e);}
      }
     }

  4. #4

    Thread Starter
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    'fraid not, the InputStreamReader has a reliance upon return.........

























    Does this work for you?

























    I've tried this code on several platforms, on all of which, it doesn't work

























    Dave.





























    Infinity isn't large, it's just incomprehensible

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You didnt say you wanted multi-platform code. It seems that you just want to capture key strokes until a certian condition occures.
    I've tried this code on several platforms, on all of which, it doesn't work
    Im pretty sure that Windows platforms terminate with a \r\n pair
    Unix '\n'(line feed) Macintosh '\r' (carriage return).

  6. #6

    Thread Starter
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    The point is though, that it doesn't matter what the chacter sequence is for the operating system, when I want to avoid it, I just want to be able to capture a single key input, with that input being the only terminator, so that I receive that and that only, from pressing that key and that key alone.


    Infinity isn't large it's just incomprehensible

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    In that case i guess i would just take the input and
    store it into a string then test the strings length to
    see if more than one character was entered. I dont
    think that Java enables you to capture only one
    keystroke then termainate. You would probably
    have to test the input then terminate manually.

  8. #8

    Thread Starter
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    Originally posted by Dilenger4
    I dont
    think that Java enables you to capture only one
    keystroke then termainate. You would probably
    have to test the input then terminate manually.
    That's exactly what I want, just to press a key, and have that key returned to me.

    This is available with a GUI, but I don't want to use one


    Infinity isn't large it's just incomprehensible

  9. #9
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You could either retrieve the command line arguements through the args array store that into a String and test the length, or a better way would be to present a prompt to the user then take the readLine() method contained in the java.io.BufferedReader class to read off of the command line, store the data in a String and test the length. The readLine() method will block indefinitely untill the user enters somthing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width