Computer Science Canada

Input of a Single Key

Author:  Nai [ Mon Aug 10, 2009 8:17 pm ]
Post subject:  Input of a Single Key

I'm trying to complete a software project for an online course that I am taking. I need to display an opening splash screen before continuing with the output. The instructor of this course has told me that I should allow the user to advance from the opening screen by pressing a key on the keyboard, and this is where my problem has come from.

The problem is that the only ways I know how to get any input from the keyboard are methods such as c.readLine(), c.readInt, c.readDouble(), and c.readChar(). (This course is unfortunately using Ready to Program, which I have heard isn't very good. Not to mention the lack of quality instruction.)

Anyway, what I need to do is allow the user to advance by pressing 1 key, no matter whether or not they press Enter. Last year when I was using Turing it was as simple as using a getch (ie. get character) to delay the output until a key was pressed.

The only way l've heard of doing this in Java is by using something called a "Key Listener," although since I am relatively new to Java and do not have a proper teacher to help me adjust to this new environment, I can't really comprehend how to use them.

So my question is: How exactly do I take in one character, and only one character, be it a single letter or a single-digit number in Ready to Program?

Thanks alot for any help you can give me!

Author:  wtd [ Mon Aug 10, 2009 11:18 pm ]
Post subject:  RE:Input of a Single Key

In order to understand KeyListener, you must first understand interfaces.

Author:  facultyofmusic [ Wed Aug 19, 2009 4:38 pm ]
Post subject:  Re: Input of a Single Key

Make a separate thread that listens for the user's keyInput using the methods that you already know (the ones that comes with RTP's console). If the key the user had inputted isn't 1, then keep on listening again (So basicly putting the listening method in a loop). And when the user DO enter 1, interrupt the splash screen.

Author:  seekingKnowledge [ Wed Oct 07, 2009 9:16 am ]
Post subject:  RE:Input of a Single Key

Hi,
have you tried using a Scanner in Java. well import java.util.* or to be specific, import java.util.Scanner;

I am assuming you have your splash running well, so just after the line of code that displays your splash you want to try to use a scanner to catch any input before the rest of the program continues... so am suggesting

Scanner sc = new Scanner(System.in);
String result = "";

while( result.equals("") )
{
result = sc.nextLine();
}

this checks to see if any character has been supplied...and breaks out if so....

well just suggesting.... (not too sure if its good for your purpose).. take care


: