PDA

Click to See Complete Forum and Search --> : True single input on command line


daveyboy
Mar 20th, 2002, 11:58 AM
Can anyone actually tell me a real platform independent way of esentially do what QBasic would have been an inkey$ (oh for the days of QBasic), I just want to grab the next key pressed, or ideally whatever key is being pressed at this precise moment in time.......

Someone suggested this:

String tmp = "";
char C='\0';
try{
while ((C=(char) System.in.read()) !='\n')
{
if (C != '\r') tmp = tmp+C;
System.out.print('\r');
}
} catch(IOException err){}


And that this would use System.in.read() to input single characters until return ('\r' within unicode) is entered.........

but it doesn't

I tried to go through the java source within System, and the classes for in (can't remember the class now), etc, but to no avail...

Any suggestions?

Dave.

PS if it looks stupid it's opera playing me up

:) :( :o :D ;) :p :cool: :rolleyes: :mad: :eek: :confused:

Infinity isn't large it's just incomprehensible

daveyboy
Mar 20th, 2002, 12:00 PM
hmmmm, also any ideas why my code section buggered up like that?













*** k I sussed this!!! ******

:) :( :o :D ;) :p :cool: :rolleyes: :mad: :eek: :confused:



Infinity isn't large, it's just incomprehensible

Dillinger4
Mar 21st, 2002, 01:16 AM
try this on for size. :p

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);}
}
}

daveyboy
Mar 21st, 2002, 03:09 AM
'fraid not, the InputStreamReader has a reliance upon return.........

























Does this work for you?

























I've tried this code on several platforms, on all of which, it doesn't work

























Dave.

























:) :( :o :D ;) :p :cool: :rolleyes: :mad: :eek: :confused:



Infinity isn't large, it's just incomprehensible

Dillinger4
Mar 21st, 2002, 02:51 PM
You didnt say you wanted multi-platform code. :p 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).

daveyboy
Mar 21st, 2002, 03:25 PM
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.

Dillinger4
Mar 21st, 2002, 03:59 PM
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.

daveyboy
Mar 21st, 2002, 04:05 PM
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

Dillinger4
Mar 21st, 2002, 04:14 PM
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.