Skip to content

Commit

Permalink
docs(keycode): add example
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Apr 15, 2020
1 parent 9869720 commit 3be5b72
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/keycode/read-key.ts
@@ -0,0 +1,31 @@
#!/usr/bin/env -S RUST_BACKTRACE=1 deno

import { KeyCode } from '../../packages/keycode/lib/key-code.ts';

async function read(): Promise<void> {

const buffer = new Uint8Array( 8 );

Deno.setRaw( 0, true );
const nread = await Deno.stdin.read( buffer );
Deno.setRaw( 0, false );

if ( nread === Deno.EOF ) {
return;
}

const data = buffer.subarray( 0, nread );

const event = KeyCode.parse( data );

if ( event && event.name === 'c' && event.ctrl ) {
console.log( 'exit' );
return;
}

console.log( event );

await read();
}

await read();

0 comments on commit 3be5b72

Please sign in to comment.