Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 17th, 2005, 8:35 AM   #1
asddsa
Newbie
 
Join Date: Jun 2005
Posts: 4
Rep Power: 0 asddsa is on a distinguished road
press any key to continue

how do i do that?
asddsa is offline   Reply With Quote
Old Jun 17th, 2005, 8:39 AM   #2
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
how do you do what?
__________________
countdown++;
HeX is offline   Reply With Quote
Old Jun 17th, 2005, 8:41 AM   #3
asddsa
Newbie
 
Join Date: Jun 2005
Posts: 4
Rep Power: 0 asddsa is on a distinguished road
"press any key to continue",
i want to continue my code just after the user presses a key...
asddsa is offline   Reply With Quote
Old Jun 17th, 2005, 8:56 AM   #4
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
you can do that with a KeyListener
look in google
__________________
countdown++;
HeX is offline   Reply With Quote
Old Jun 17th, 2005, 5:15 PM   #5
asddsa
Newbie
 
Join Date: Jun 2005
Posts: 4
Rep Power: 0 asddsa is on a distinguished road
can you help me with this one...
i found KeyListener just in a new window (JFrame), and i want to use it in the "dos" window...
asddsa is offline   Reply With Quote
Old Jun 17th, 2005, 6:17 PM   #6
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
It's a bit of a hack, but you could create a "ghost" Component that has a KeyListener registered to it. You don't need to actually show this component or even use it for anything else, just have it listen in for KeyEvents.

These might help:
Sun's tutorial on KeyListeners
The Java API (look up KeyListener and Component)
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Old Jun 17th, 2005, 9:48 PM   #7
VigilanteP@comcast.net
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 VigilanteP@comcast.net is on a distinguished road
Nevermind --- my suggestion didnt work.

Last edited by VigilanteP@comcast.net; Jun 17th, 2005 at 9:53 PM.
VigilanteP@comcast.net is offline   Reply With Quote
Old Jun 18th, 2005, 5:37 AM   #8
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 5 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
define a function like this (blatantly stolen from savitch)
 /**
     Reads a line of text and returns that line as a String
     value. The end of a line must be indicated either by a
     new-line character '\n' or by a carriage return '\r'
     followed by a new-line character '\n'. (Almost all systems
     do this automatically. So you need not worry about this
     detail.) Neither the '\n', nor the '\r' if present, are
     part of the string returned. This will read the rest of a
     line if the line is already partially read.
    */
    public static String readLine( )
    {
        char nextChar;
        String result = "";
        boolean done = false;

        while (!done)
        {
            nextChar = readChar( );
            if (nextChar == '\n')
               done = true;
            else if (nextChar == '\r')
            {
                //Do nothing.
                //Next loop iteration will detect '\n'.
            }
            else
               result = result + nextChar;
        }

        return result;
    }

then do somthing like:
System.out.println("Press any key to continue.");
readLine();

that readline function will work for most any string based input.
good luck!
ZenMasterJG is offline   Reply With Quote
Old Jun 18th, 2005, 12:51 PM   #9
VigilanteP@comcast.net
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 VigilanteP@comcast.net is on a distinguished road
"Press any key to continue" ..

Won't your loop only terminate if the user enters a newline character (enter)?
VigilanteP@comcast.net is offline   Reply With Quote
Old Jun 18th, 2005, 4:57 PM   #10
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Correct, this code only works when "enter" is pressed. Actually, it doesn't work at all beause readChar() isn't actually defined, but we'll overlook that for a minute. The real problem with trying to read from an input stream (esp. System.in) is that control isn't returned to the main thread until the return key is pressed anyway. You can hit all the keys and enter all the characters you want, but none of them will actually be read until you hit return and give contol back to the program.

To be fair, the method I suggested earlier about using a "ghost" Component won't work either. I forgot that Components need to have focus before they can listen in for KeyEvents, and they can't have focus unless they're visible.

In short, I don't think what you're trying to do is possible; not in Java anyway. I say just go with "Press Enter to Continue" and be done with it.
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?

Last edited by EdSalamander; Jun 18th, 2005 at 5:02 PM.
EdSalamander is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC