![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 4
Rep Power: 0
![]() |
press any key to continue
how do i do that?
|
|
|
|
|
|
#2 |
|
Programmer
|
how do you do what?
__________________
countdown++; |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 4
Rep Power: 0
![]() |
"press any key to continue",
i want to continue my code just after the user presses a key... |
|
|
|
|
|
#4 |
|
Programmer
|
you can do that with a KeyListener
look in google
__________________
countdown++; |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2005
Posts: 4
Rep Power: 0
![]() |
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... |
|
|
|
|
|
#6 |
|
Programmer
|
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? |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
Nevermind --- my suggestion didnt work.
Last edited by VigilanteP@comcast.net; Jun 17th, 2005 at 9:53 PM. |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
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! |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
"Press any key to continue" ..
Won't your loop only terminate if the user enters a newline character (enter)? |
|
|
|
|
|
#10 |
|
Programmer
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|