-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Presses of special keys are not detectable by Deno on Windows #5945
Comments
@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? |
Can reproduce the issue windows for me - arrow keys arent being logged |
Any update about this issue? seems like in 1.8.x it is not possible still |
The issue is still open, which means it isn't fixed. |
Running into this issue as well. Is there any working progress? |
Hi @x7y62 I did this module for handling native input events, check it out if you need to solve this https://github.com/denyncrawford/deno-gkm It's in dippers but it can do the job. Hope it helps. |
Until this issue is resolved, here is yet another quick work-around using Deno's new FFI capability. No other libraries are required, but you do need the Windows command line C/C++ 64-bit complier CL (could probably instead call Windows DLL functions directly, if anyone wants to try that route). This is not async, and uses the old Windows console function getch(). kbhit() is also provided, if needed, to check whether a keypress is available, since getch() is blocking. Save this as getch.c, and compile from x64 Visual Studio command prompt with cl /LD getch.c to generate getch.dll: #include <conio.h>
__declspec(dllexport) int __getch() {return _getch();}
__declspec(dllexport) int __kbhit() {return _kbhit();} Example JS/TS code for testing, save as getch.js (or ts), and invoke with deno run --allow-ffi --unstable getch.js: const dylib = Deno.dlopen("getch.dll", {
"__getch": { parameters: [], result: "i32" },
"__kbhit": { parameters: [], result: "i32" },
});
while(true) {
let ch = dylib.symbols.__getch();
console.log(`Key pressed: ${ch}`);
if(ch == 3) break;
} Now you will get the 2-byte codes for keypad, arrow, and function keys. This works identically on Console, Windows Terminal, PowerShell, etc. |
Well, Duh, you can also simply use c:\windows\system32\msvcrt.dll (the C runtime DLL) directly, and avoid needing the compiler: const dylib = Deno.dlopen("c:\\windows\\system32\\msvcrt.dll", {
"_getch": { parameters: [], result: "i32" },
"_kbhit": { parameters: [], result: "i32" },
}); Or use the standard environment variable to find system root (then must add --allow-env to deno command line): const SysRoot = Deno.env.get("SystemRoot");
const dylib = Deno.dlopen(`${SysRoot}\\system32\\msvcrt.dll`, {
"_getch": { parameters: [], result: "i32" },
"_kbhit": { parameters: [], result: "i32" },
}); |
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 ofDeno.setRaw()
):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.
The text was updated successfully, but these errors were encountered: