Por qué CTRL + METRO da un valor ASCII de 10 (valor decimal). En realidad debería dar 13. Me estoy conectando a la instancia de linux de Amazon EC2 a través de masilla. Ejecuto el siguiente programa
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
public class NumbersConsole {
private static String ttyConfig;
public static void main(String[] args) {
try {
setTerminalToCBreak();
int i=0;
while (true) {
//System.out.println( ""+ i++ );
if ( System.in.available() != 0 ) {
int c = System.in.read();
System.out.println(c);
if ( c == 13 ) {
break;
}
}
} // end while
}
catch (IOException e) {
System.err.println("IOException");
}
catch (InterruptedException e) {
System.err.println("InterruptedException");
}
finally {
try {
stty( ttyConfig.trim() );
}
catch (Exception e) {
System.err.println("Exception restoring tty config");
}
}
}
private static void setTerminalToCBreak() throws IOException, InterruptedException {
ttyConfig = stty("-g");
// set the console to be character-buffered instead of line-buffered
stty("-icanon min 1");
// disable character echoing
stty("-echo");
}
/**
* Execute the stty command with the specified arguments
* against the current active terminal.
*/
private static String stty(final String args)
throws IOException, InterruptedException {
String cmd = "stty " + args + " < /dev/tty";
return exec(new String[] {
"sh",
"-c",
cmd
});
}
/**
* Execute the specified command and return the output
* (both stdout and stderr).
*/
private static String exec(final String[] cmd)
throws IOException, InterruptedException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Process p = Runtime.getRuntime().exec(cmd);
int c;
InputStream in = p.getInputStream();
while ((c = in.read()) != -1) {
bout.write(c);
}
in = p.getErrorStream();
while ((c = in.read()) != -1) {
bout.write(c);
}
p.waitFor();
String result = new String(bout.toByteArray());
return result;
}
}
y cuando doy la entrada comoCTRL + METRO), Estoy mostrando un valor de 10. Pero estoy esperando un valor de 13. ¿Por favor, hágame saber si me falta algo?
Respuestas
3 para la respuesta № 1La traducción de CR a LF es manejada por el controlador tty. Estas llamando setTerminalToCBreak()
, que manipula la configuración de tty (creo que deshabilita los caracteres especiales de borrar, kill, werase y rprnt).
los icrnl
La configuración, que está habilitada de forma predeterminada, hace que el retorno de carro (CR) se traduzca a nueva línea (LF). Desactivar eso debería permitirte ver los caracteres CR directamente. Ajuste raw
el modo cambia una serie de indicadores, incluido el apagado icrnl
. (Descubrir cómo hacer eso en Java se deja como un ejercicio).
Pero ten cuidado de hacer esto. los Entrar o Regreso La clave normalmente envía un carácter CR. Traducirlo a LF es lo que le permite marcar el final de una línea. Si desactiva esa traducción, podría interrumpir ese comportamiento a menos que usted mismo maneje CR.
Para obtener más información sobre la configuración de tty, man tty
o seguir este enlace.
0 para la respuesta № 2
Mi otra respuesta comenzó en la página totalmente incorrecta.
stty ("-cooked")
funciona para mi.
Algo en las profundidades de la tierra del teletipo quiere que tengas un poco feliz ^J
s en lugar de ^M
s, pero la cocción de la terminal lo detiene.
$ stty -cooked ; java -cp /tmp NumbersConsole
13
$
De vuelta en los Días buenos, algunas computadoras (Commodore, Apple) usaron ^ M (13) para su clave de retorno; algunas (IBM) usaron una combinación ^ M ^ J; otras (Unix) usaron ^ J (10).
Ahora, en el mundo moderno, es casi siempre ^ J (aunque creo que el código de Windows todavía tiene algunas cosas heredadas ^ M ^ J debajo del capó).