Results 1 to 6 of 6

Thread: How to get a character from input stream without pressing RETURN key?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    12

    Question How to get a character from input stream without pressing RETURN key?

    In C, we could use getch() form <conio.h> to get a character from the input steam without pressing the return/enter key. How could I tranlate this function for use in Java?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
     
        import java.awt.event.*;   
    
         class Keyboard extends KeyAdapter{
         
          public void keyPressed(KeyEvent ke){
           Character text = new Character(ke.getKeyChar());
           jta.append(text.toString()); 
         }
       }

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    12
    Hi <Dilenger4>, is the class that you suggested used for instantiating a Listener object that must be in associating with a GUI object?

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    12

    Wink

    Alright, so this means that I can't do it purely in the console DOS mode without the help of a GUI. Anyway, thanx a lot for your help!

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You could do somthing like this.
    Just don't hit the enter key until
    you want to exit back to the prompt
    or you will just get a lot of numbers
    that could interfere with your program.
    Code:
      import java.io.*; 
      
      public class Grabber{
      public static void main(String[] args){
           
      for(;;){
       try{
        int data = System.in.read();
        if(data == 27){ // esc key
         break;  
       }
        System.out.print(data);
       }catch(IOException e ){}
      }
     }
    }

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