Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

"Press any Key to Continue" - Console

843789Feb 17 2010 — edited Feb 19 2010
Does anyone have any ideas on how to approach this? From searching, everyone says that in Java a keylogger is not possible, because the read() method and its derivatives would still require a newline for input. This means that the user needs to press Enter.

Someone suggested Thread, but I was only up to the point where I call interrupt() on the single thread and I got lost afterward.

The workaround is just to ask the user to press Enter
public boolean pressEnterToContinue() {
        boolean enterIsPressed = false;
        System.out.println("Press Enter to continue");
       
        try {
            System.in.read();
            enterIsPressed = true;
        }
        catch (IOException e) {
            System.out.println("Program error.");
            System.exit(1);
        }
        return enterIsPressed;
    }
but if you have any other suggestions on how to go about it, I would appreciate it.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 19 2010
Added on Feb 17 2010
2 comments
191 views