并发/非阻塞控制台键盘输入 [英] Concurrent/Non-blocking console keyboard input

查看:184
本文介绍了并发/非阻塞控制台键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java中的MUD。我每次打勾都会看到玩家输入,但我使用的是 Scanner ,它使用了阻止操作。我想要非阻塞输入。

I'm working on a MUD in java. I read player input every tick, but I'm using Scanner which uses blocking operations. I want to have non-blocking input.

我查看了 nio 包,其中包含选择器 class,但我不知道如何使用它来关于 System.in 。我想我在运行服务器时肯定需要它,但现在一切都处于脱机状态。

I've looked at the nio package which has a Selector class, but I'm not sure how to use it with regard to System.in. I figure I'll definitely need it once I'm running a server, but for now everything is offline.

我试过从<$ c扩展主类$ c> Applet 并覆盖 keyDown ,但这只是意味着在第一个之后不再接受输入。当然,我不再阻止任何东西了,但是没有更多的输入。我猜是 keyDown 再也没有被调用过。

I've tried extending the main class from Applet and overriding keyDown, but that just meant input was no longer accepted after the first one. Sure, I wasn't blocking anything anymore, but then there was no more input. keyDown never got called again, I guess.

也许线程即使在执行阻塞操作时也会被中断?

Perhaps threads can be interrupted even when they are executing blocking operations?

感谢您对此问题的任何见解。

Thanks for any insight into this problem.

推荐答案

您可以不能用系统控制台做到这一点,因为现在无法以多平台方式完成。

You can't do that with the system console because by now it can't be done in a multi-platform way.

您可以使用swing窗口作为控制台或查找JNI基于方法,但它可能在某些平台上不起作用。

You can use swing window as console or find a JNI based approach but it might not work on some platforms.

您可以使用 JCurses 。它可能有用,它基于JNI并支持Windows和Linux。

You may use JCurses. It might work, it's based on JNI and supports Windows and Linux.

这篇关于并发/非阻塞控制台键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆