It is currently impossible to know when the user presses a special key in Console Window Host (the default terminal, conhost.exe) or Windows Terminal.
Here's a test script (needs --unstable because of Deno.setRaw()):
const stdinRid = Deno.stdin.rid;
if (Deno.isatty(stdinRid))
Deno.setRaw(stdinRid, true);
(async () => {
for await (const data of Deno.iter(Deno.stdin)) {
if (data[0] === 0x03) // Ctrl+C
Deno.exit();
console.log(data);
}
})();
This could log an escape sequence when the user presses, for example, the arrow keys (I think it does on Linux), but it logs nothing.
@dd-pardal I get odd behavior from yours as well on Mac, have you tried:
if (!Deno.isatty(Deno.stdin.rid)) {
console.log("Cannot do raw input when not tty");
Deno.exit(-1);
}
Deno.setRaw(Deno.stdin.rid, true);
while (true) {
const buf = new Uint8Array(512);
const bytesRead = await Deno.read(Deno.stdin.rid, buf);
console.log(bytesRead);
console.log(buf);
if (bytesRead === 1 && buf[0] === 0x03) {
break;
}
}
Deno.setRaw(Deno.stdin.rid, false);
Any updates on that? Is there at least some workaround?
Most helpful comment
Any updates on that? Is there at least some workaround?