title: Is there a way to know if a user pressed the key ENTER using readline() method? - Java
body: <p>I want to recognize if a user sent a text, using readline method from BufferedReader. While the user didn't send anything, the Thread keeps sleeping. Is there a way to do this? I'm trying for a long time, but no success yet.. I guess I have to recognize if a ENTER key was pressed.. but I donwt know how to do this.. The interface of my program is the console.. </p>
  
  <p>The problem is: I have a client-server program. When a client enters a message, the message prompts on the screen of the another user/client. But, if the user doesn't send a message in 10 seconds, I want a message to appear on the screen.. So I have a Thread.sleep(1000). The problem is I don't know when to wakeuo the Thread. </p>
  
  <pre><code>while((mensagem == null) || (!mensagem.trim().equals(""))) { 
        Thread.sleep(10000); 
        sendToAll(output, "[Your turn] ", ""); 
        mensagem = input.readLine(); 
     }
  </code></pre>
  
  <p>The last line isn't correct also, because I don't want to force the user to type, and this way I'm forcing, 'cause I stop on input.readLine().</p>
  
  <p>SOLUTION:
  I resolved the problem using the method setSoTimeout(10000).. This is the code:</p>
  
  <pre><code>public Server(Socket socket) throws SocketException{
     this.connection = socket;
     this.connection.setSoTimeout(10000);
  }
  
  public void run(){
     [...]
     while((msg != null) &amp;&amp; !(msg.trim().equals(""))){        
        try{  
           msg = input.readLine();
       sendToAll(output, ": ", msg);  
        } catch (SocketTimeoutException e) {
       sendToAll(output, "Time out ", "");
        }       
  }
  </code></pre>
  
  <p>Thank you all for the ideas!! :)</p>