jakeprogrammer
jakeprogrammer

Reputation: 152

Java check if user is typing in the console

Is there a way to check if the user is typing in the console window in Java? My program prints information it receives about client connections using System.out.print() and I want it to stop printing information temporarily while the user is typing. User input is read on a separate thread using the Scanner class. I need to be able to see if the user has typed anything and still have whatever the user has typed (if anything) available to the scanner. If it's possible I would like to avoid using external libraries and just stick with the java libraries.

Upvotes: 2

Views: 1510

Answers (3)

ddmps
ddmps

Reputation: 4390

No, there are no ways to do that directly using the console (that I'm aware of) - the content of the console will only get sent to the application once enter is pressed and will then be available to the Scanner.

One way to solve it is to make your own console, that you can read and write from. Then you'll be able to do anything you want really (including check if anything is highlighted and so on). If you don't know how to code a GUI, you should look into that. Oracle has a tutorial on GUI with Swing.

Upvotes: 2

Galen Nare
Galen Nare

Reputation: 336

I think you're looking for Thread.sleep(milliseconds);. Using this until the user is done, you should get what you want.

Next time, please elaborate on your questions.

Upvotes: 0

Akash Yadav
Akash Yadav

Reputation: 2421

The thread that listens to the user console , Generates an interrupt when the user starts typing, while the other thread that prints the information need to reset the interrupt, all you have to do is implementation of the scenario.

Upvotes: 0

Related Questions