Sean McCloskey
Sean McCloskey

Reputation: 21

How do I wait for and read a single key input in the Console?

I am creating a game, and just to get the logic down I am only using the console window to test the process. I am prompting the user to hit either the 'd' or the 'p' key without requiring to press the Enter key. If the user presses either one it will branch off to its subroutine. How do I go about waiting for the input of a single key?

This is in Java. Sorry for the confusion

Upvotes: 2

Views: 8170

Answers (3)

LINEMAN78
LINEMAN78

Reputation: 2562

System.in.read() is a blocking read on a single unsigned byte.

Upvotes: 1

Cole Smith
Cole Smith

Reputation: 307

It sounds like you want to read a keypress without waiting for an "enter" or newline. Unfortunately this isn't really possible in Java, as detailed in this thread. I wish I could help you more, but there aren't really any portable ways of doing it.

Upvotes: 2

sethupathi.t
sethupathi.t

Reputation: 502

Include the below line where your program want to wait to get input from user.

DataInputStream input = new DataInputStream(System.in); String string = input.readLine();

For more information refer the below site

http://www.roseindia.net/java/java-get-example/java-get-user-input.shtml http://www.daniweb.com/software-development/java/threads/85435

Upvotes: 0

Related Questions