Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent/Non-blocking console keyboard input

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.

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.

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.

like image 849
Robert Qualls Avatar asked Feb 12 '10 04:02

Robert Qualls


1 Answers

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

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

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

like image 67
aalku Avatar answered Nov 16 '22 13:11

aalku