David
David

Reputation: 1296

Java Stream Keyboard Input

I am writing a program that takes commands via the console.

However, I do not want to press "enter" in order to send that command.

I want the code to constantly monitor what I am entering, and execute the code when I am finished.

The commands are coming as text from a speech recognition program, therefore, eliminating the need for the "enter" stroke is pretty key.

Any one have any ideas?

Upvotes: 5

Views: 1936

Answers (3)

Eric Giguere
Eric Giguere

Reputation: 3505

Already answered here:

How to read a single char from the console in Java (as the user types it)?

No portable way to do it, depends on your platform.

Upvotes: 1

Babak Naffas
Babak Naffas

Reputation: 12561

I haven't worked with Java since college, but if you're reading from the command line, then you are using System.in.read() or something similar. Since these are blocking method calls, your application will never be notified about the new input until ENTER is pressed.

If I'm dead wrong, please let me know.

You're probably better off using a simple (possibly even hidden) that can take the user text and an event listener on the UI element that reads it in to the application's text processor.

Upvotes: 0

prolink007
prolink007

Reputation: 34544

I have recently come to the knowledge of events in java and i believe this would help you. You would just need to associate the speech recognition printing to the screen with an event in java and it would have a listener to listen for the event and when it sees the event it would execute your desired code. I currently have a thread opened where im trying to get some good examples of this, perhaps that will help.

Java Events Question

Upvotes: 1

Related Questions