Computer Science Canada

If a key is pressed...

Author:  atal [ Sun Sep 30, 2012 3:58 pm ]
Post subject:  If a key is pressed...

So I am just working on a true and false game for fun and I want to start it like this. (I'm using a console in RTP btw).

What I want is an if statement where it would be like


c.println("Press any key to continue")
if [CODE FOR KEY PRESSED] {
game here}

something like that. So how would I do that for differet keys like "T" "F" "S" "Enter" and so on.

Author:  TerranceN [ Mon Oct 01, 2012 10:17 pm ]
Post subject:  Re: If a key is pressed...

I believe you're looking for the getChar method of the Console. It blocks execution (ie. it waits until you enter a character) and returns what was entered.

Example:

Java:

import hsa.Console;

public class WaitForKey {
    public static void main(String[] args) {
        Console c = new Console();
        c.println("Press any key to continue!");
        char charPressed = c.getChar();
        c.println("You pressed " + charPressed);
    }
}


I would have told you to go read the documentation, but surprisingly, and unlike Turing, there doesn't seem to be built in docs for hsa.Console. Am I just blind, or is there really no docs?

Author:  QuantumPhysics [ Tue Oct 02, 2012 11:22 pm ]
Post subject:  RE:If a key is pressed...

The oracle website has a nice one for hsa.console - Aside from that, no.


: