Node.js Raw Mode with Keystrokes

By  on  

I find the stuff that people are doing with Node.js incredibly interesting.  You here about people using Node.js to control drones, Arduinos, and a host of other devices.  I took advantage of Node.js to create a Roku Remote, a project that was fun and easier than I thought it would be.  There was one piece of this experiment that was difficult, however:  listening for keystrokes within the same shell that executed the script.

The process for using the remote is as follows:

  1. Execute the script to connect to your Roku:  node remote
  2. In the same shell, use arrow keys and hot keys to navigate the Roku
  3. Press CONTROL+C to kill the script

The following JavaScript code is what I needed to use to both listen for keystrokes within the same shell once the script had been started:

// Readline lets us tap into the process events
const readline = require('readline');

// Allows us to listen for events from stdin
readline.emitKeypressEvents(process.stdin);

// Raw mode gets rid of standard keypress events and other
// functionality Node.js adds by default
process.stdin.setRawMode(true);


// Start the keypress listener for the process
process.stdin.on('keypress', (str, key) => {

    // "Raw" mode so we must do our own kill switch
    if(key.sequence === '\u0003') {
        process.exit();
    }

    // User has triggered a keypress, now do whatever we want!
    // ...

});

The code above turns your Node.js script into an active wire for listening to keypress events.  With my Roku Remote, I pass arrow and letter keypress events directly to the Roku via a REST API (full code here).  I love that Node.js made this so easy -- another reason JavaScript always wins!

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    Ana Tudor’s Favorite CodePen Demos

    Cocoon I love canvas, I love interactive demos and I don't think I have ever been more impressed by somebody's work than when I discovered what Tiffany Rayside has created on CodePen. So I had to start off with one of her interactive canvas pens, even though...

  • By
    Resize an Image Using Canvas, Drag and Drop and the File API

    Recently I was asked to create a user interface that allows someone to upload an image to a server (among other things) so that it could be used in the various web sites my company provides to its clients. Normally this would be an easy task—create a...

Discussion

  1. Haldun

    I like a lot that you are writing about messing with Iot using nodejs! Looking forward to see more! Thanks!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!