|
-
Mar 20th, 2002, 11:58 AM
#1
Thread Starter
Member
True single input on command line
Last edited by daveyboy; Mar 20th, 2002 at 01:33 PM.
-
Mar 20th, 2002, 12:00 PM
#2
Thread Starter
Member
-
Mar 21st, 2002, 01:16 AM
#3
Dazed Member
try this on for size.
Code:
import java.io.*;
public class Read {
public static void main(String[] args){
InputStreamReader isr = new InputStreamReader(System.in);
StringBuffer sb = new StringBuffer();
try{
while(true){
char c = (char) isr.read();
sb.append(c);
if(c =='\r'){
System.out.println("Enter was pressed!");
System.out.println(" Keys that were pressed so far: " + sb);
break;
}
}
}catch(IOException e){System.err.println(e);}
}
}
Last edited by Dilenger4; Mar 21st, 2002 at 01:31 AM.
-
Mar 21st, 2002, 03:09 AM
#4
Thread Starter
Member
-
Mar 21st, 2002, 02:51 PM
#5
Dazed Member
You didnt say you wanted multi-platform code. It seems that you just want to capture key strokes until a certian condition occures.
I've tried this code on several platforms, on all of which, it doesn't work
Im pretty sure that Windows platforms terminate with a \r\n pair
Unix '\n'(line feed) Macintosh '\r' (carriage return).
-
Mar 21st, 2002, 03:25 PM
#6
Thread Starter
Member
The point is though, that it doesn't matter what the chacter sequence is for the operating system, when I want to avoid it, I just want to be able to capture a single key input, with that input being the only terminator, so that I receive that and that only, from pressing that key and that key alone.
-
Mar 21st, 2002, 03:59 PM
#7
Dazed Member
In that case i guess i would just take the input and
store it into a string then test the strings length to
see if more than one character was entered. I dont
think that Java enables you to capture only one
keystroke then termainate. You would probably
have to test the input then terminate manually.
-
Mar 21st, 2002, 04:05 PM
#8
Thread Starter
Member
Originally posted by Dilenger4
I dont
think that Java enables you to capture only one
keystroke then termainate. You would probably
have to test the input then terminate manually.
That's exactly what I want, just to press a key, and have that key returned to me.
This is available with a GUI, but I don't want to use one
-
Mar 21st, 2002, 04:14 PM
#9
Dazed Member
You could either retrieve the command line arguements through the args array store that into a String and test the length, or a better way would be to present a prompt to the user then take the readLine() method contained in the java.io.BufferedReader class to read off of the command line, store the data in a String and test the length. The readLine() method will block indefinitely untill the user enters somthing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|