Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 If a key is pressed...
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
atal




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: 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?
QuantumPhysics




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: