简体   繁体   English

Java Stream键盘输入

[英]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? 有人有主意吗?

I have recently come to the knowledge of events in java and i believe this would help you. 我最近了解了java中的事件,我相信这会对你有所帮助。 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. 您只需要将语音识别打印与屏幕中的事件关联到java中,它就会有一个监听器来监听事件,当它看到事​​件时它将执行您想要的代码。 I currently have a thread opened where im trying to get some good examples of this, perhaps that will help. 我目前有一个线程打开,我试图得到一些很好的例子,也许这将有所帮助。

Java Events Question Java事件问题

Already answered here: 已在这里回答:

How to read a single char from the console in Java (as the user types it)? 如何在Java中从控制台读取单个字符(用户键入它)?

No portable way to do it, depends on your platform. 没有可移植的方式,取决于您的平台。

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. 我从大学开始就没有使用Java,但是如果你是从命令行读取的,那么你正在使用System.in.read()或类似的东西。 Since these are blocking method calls, your application will never be notified about the new input until ENTER is pressed. 由于这些是阻塞方法调用,因此在按下ENTER之前,永远不会通知您的应用程序有关新输入的信息。

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. 你可能最好使用一个简单的(可能是隐藏的)来获取用户文本和UI元素上的事件监听器,将其读入应用程序的文本处理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM