I want to determine cursor position in a Java console application.
This can be achieved by the following ANSI code sequence:
ESC[6n
If I send that ANSI escape command I get the answer, but I can not retrieve it in Java. This is what I have tried:
public class ConsoleTest {
public static final String ANSI_GET_CURSOR_POSITION = "\u001B[6n";
public ConsoleTest() {
System.out.println(ANSI_GET_CURSOR_POSITION);
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
String str = null;
do {
try {
str = obj.readLine();
System.out.println(str);
} catch (IOException ex) {
Logger.getLogger(ConsoleTest.class.getName()).log(Level.SEVERE, null, ex);
}
} while(!str.equals("stop"));
}
}
This is what I get, even pressing ENTER does nothing:
% java -jar ConsoleTest.jar
^[[24;1R
stop
stop
So apparently, console answers with the code but I can not read it in the Java application.