6 Replies Latest reply: Jan 26, 2004 12:00 PM by 807582 RSS

    how to read character input as soon as user enters

    807582
      Hi All !

      I want to be able to read any character from the command line console as soon as the user types/presses a key. I want to do this without using GUI. What API to use? Pointers to direction in solving this would be helpful.

      Appreciate your help.

      Regards,
      Nikunj
        • 1. Re: how to read character input as soon as user enters
          807582
          On UNIX-like systems, look at 'stty -raw' command to be issued before you run your java program. This disables the system from editing data coming from the terminal (EG processing backspaces)
          • 2. Re: how to read character input as soon as user enters
            807582
            How about this :
            int i = System.in.read();
            while (i != -1) {
              // here's your character
              char c = (char) i;
              i = System.in.read();
            }                                                                                                                                                                                                                                                                                            
            • 3. Re: how to read character input as soon as user enters
              807582
              Oops! Try this :
              int i = System.in.read();
              while (i != -1) {
                // here's your character
                char c = (char) i;
                i = System.in.read();
              }
              • 4. Re: how to read character input as soon as user enters
                807582
                Thaks dpz! I will try out your solution.

                Thanks serlank. But I think this will still involve the user to type a char and then press return key?? won't it? My application requires that the char data that the user just entered (as he is typing) is being sent to another module. So i want to send all chars ( while the user is still typing the string) in 'real-time' (sort-of).

                Do u think this solution would still work? I thought otherwise, but pls correct me if I am wrong.

                Appreciate all your help. This thing is quite trivial in C but i am not sure what is the best way to achieve this in Java. Constraint being that I do not want to use GUI.

                Thanks,
                Nikunj
                • 5. Re: how to read character input as soon as user enters
                  807582
                  Have a look at java.io.InputStream in the API, then have a look at the method abstract int read().

                  This should answer your question, and if it doesn't try it. 5 lines of code isn't that much to write.
                  • 6. Re: how to read character input as soon as user enters
                    807582
                    My Bad!! Just tried it myself and saw your problem. Will get back to you.