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
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • By
    Create WordPress Page Templates with Custom Queries

    One of my main goals with the redesign was to make it easier for visitors to find the information that was most popular on my site. Not to my surprise, posts about MooTools, jQuery, and CSS were at the top of the list. What...

  • By
    CSS pointer-events

    The responsibilities taken on by CSS seems to be increasingly blurring with JavaScript. Consider the -webkit-touch-callout CSS property, which prevents iOS's link dialog menu when you tap and hold a clickable element. The pointer-events property is even more JavaScript-like, preventing: click actions from doing...

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!