1

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.

k-a-v
  • 326
  • 5
  • 22
  • Have you seen these: [Equivalent function to C's “_getch()” in Java?](https://stackoverflow.com/questions/1864076/equivalent-function-to-cs-getch-in-java) and [Non-Blocking Input in Java realized through JNI](https://stackoverflow.com/questions/15110117/non-blocking-input-in-java-realized-through-jni) – Abra Jan 06 '20 at 02:49

1 Answers1

1

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);
HuDeanY
  • 33
  • 5