9

I've just started messing around with JLine to parse character input in console mode. It seems to work well, but I'm wondering:

Is there a nonblocking way in JLine to find out if characters are available? (i.e. like kbhit() in Windows.)

I suppose I could always wrap keyboard input in its own thread which then offers the keyboard characters in a thread-safe queue to the main thread, but that seems like it should be unnecessary.

EDIT: This is character-by-character parsing. I am not going to use a GUI. The usual InputStream I/O in Java in console mode requires you to hit the Enter key first (e.g. it's buffered input only). Please don't tell me character-by-character input in console mode is impossible in Java; it isn't. JLine does it using a portable interface with a platform-dependent implementation.

Edit update: I was able to hack together a helper class to do the blocking I/O in a worker thread (using JLine for the per-character I/O, warning: you have to parse Ctrl-C yourself!) & then communicate via a synchronized queue with an isempty() routine. For what I'm doing right now that's fine, but I would really like to know a Good Way To Do This In The Future.

1
  • 6
    Could you post an snipped of the answer? Looks very interesting. Commented Jan 9, 2009 at 20:40

2 Answers 2

4

You seem to be on the right track.

I think the "right" way to do this is a worker thread that pours all the blocking I/O into a non-blocking queue. Hava a look at ConcurrentLinkedQueue from java.util.concurrent.

Sign up to request clarification or add additional context in comments.

Comments

3

You can't use a console to get non-blocking input without native libraries.

You'll have to write a Swing app and write a KeyListener

Read this tutorial: http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.