I want to get the position of the terminal with the ANSI escape code \\033[6n
. I do this with a simple:
System.out.print("\033[6n");
This will put a response, like [[33;225R
in stdin. How do I get this response? I've tried System.in.read()
, but this waits for the user to press enter, which I don't want.
On Unix Systems use:
Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", "stty raw -echo </dev/tty" }).waitFor();
System.out.print("\033[6n");
Reader inputReader = System.console().reader();
int byteBuffer;
StringBuilder buffer = new StringBuilder();
while ((byteBuffer = inputReader.read()) > -1) {
if (byteBuffer == 3) {
break;
} else if (byteBuffer == 27) {
buffer.append("\\033");
} else {
buffer.append((char)byteBuffer);
if ('R' == byteBuffer) {
break;
}
}
}
Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", "stty -raw echo </dev/tty" }).waitFor();
System.out.println("Input: " + buffer);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.