Deno keypress reader

Reads key from stdin.

Try

deno run --unstable https://raw.githubusercontent.com/dmitriytat/keypress/master/readKeypress_test.ts

// CTRL + C keypress
const keypress: Keypress = {
  type: "keypress",
  key: "c",
  code: undefined,
  keyCode: 3,
  sequence: "",
  unicode: "\u0003",
  ctrlKey: true,
  metaKey: false,
  shiftKey: false
}

Usage

Read from Deno.stdin by default:

import { readKeypress } from "./mod.ts";

for await (const keypress of readKeypress()) {
    console.log(keypress);

    if (keypress.ctrlKey && keypress.key === 'c') {
        Deno.exit(0);
    }
}

Read from TTY:

import { readKeypress } from "./mod.ts";

const tty = await Deno.open("/dev/ttys003");

for await (const keypress of readKeypress(tty)) {
    console.log(keypress);

    if (keypress.ctrlKey && keypress.key === 'c') {
        Deno.exit(0);
    }
}

Big thanks to Nathan Rajlich and his https://github.com/TooTallNate/keypress, whitch code for key decode I took to this library.