Logo

dev-resources.site

for different kinds of informations.

Keyboard input in Node.js

Published at
1/6/2024
Categories
javascript
node
core
vanilla
Author
sanjarcode
Categories
4 categories in total
javascript
open
node
open
core
open
vanilla
open
Author
10 person written this
sanjarcode
open
Keyboard input in Node.js

This article talks about working with keyboard input in Node.js. This code will run in the terminal.

We'll be discussing the default way in Node to do this. Practically, people prefer using a library like inquirer to create interactive CLIs.


The readline module can be used for reading input into programs.
A promise version readline/promises is it's marked experimental. Works by default in v20.

The APIs of the module allow for very granular control if needed - like position of cursor, row/column position, reading a line vs per character reads.

I'll demonstrate two commonly needed interfaces:

  1. Read a sentence (until user presses enter). Like C++'s cin.
  2. Read single character

1. Read sentence (until 'Enter' is pressed)

Since we get back something once, I'm using async-await.

Logic (snippet)

const readline = require("readline/promises");

/**
 * Take sentence input, until you press 'Enter'
 * Like C++ cin
 *
 * @param {String} message
 * @returns {String}
 */
const prompt = async (message) => {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  const answer = await rl.question(message);

  rl.close(); // stop listening
  return answer;
};
Enter fullscreen mode Exit fullscreen mode

A simple program

// copy code from above here
async function simpleSum() {
  const a = await prompt("Enter first number: ");
  const b = await prompt("Enter second number: ");
  console.log("Sum is", Number(a) + Number(b));
}
simpleSum();
Enter fullscreen mode Exit fullscreen mode

A more interesting program

async function getGitHubName() {
  const username = await prompt("GitHub username: ");
  const resp = await fetch(`https://api.github.com/users/${username}`);
// yes, `fetch` is available by default in Node v20
  if (!resp.ok) {
    console.log(
      "Error occurred",
      ",",
      "Code:",
      resp.status,
      ",",
      "Message:" + (await resp.text())
    );
  } else {
    const data = await resp.json();
    const name = data.name;
    console.log("User found. Name:", name);
  }
}

getGitHubName();
Enter fullscreen mode Exit fullscreen mode

2. Run code for single character

More like run some code on keypress. This continuously listens for keypresses.
Since each keypress runs some code, I'm using callback.

Example: this is what Metro (React native dev process) runs like - when 'r' is pressed it reloads.

Logic (snippet)

const readline = require("readline/promises");
/**
 * Continues listening for keypresses, and run code for each keypress
 */
const listenKeyPresses = (callback = (key, data) => console.log({ key, data })) => {

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  rl.input.on("keypress", callback);
  return rl;
};

listenKeyPresses.example = () => {
  listenKeyPresses((key, data) => {
    const isLetter =
      key.toLowerCase().charCodeAt() >= "a".charCodeAt() &&
      key.toLowerCase().charCodeAt() <= "z".charCodeAt();

    console.log(`\b${key} is a ${isLetter ? "letter" : "Non-letter"}`);
    console.log(data);
  });
};
listenKeyPresses.example() // run the example
Enter fullscreen mode Exit fullscreen mode

This is very handy when testing, trying something new. Instead of creating a small frontend using HTML, CSS or JS, or setting up Postman. Just use this, setup key press and corresponding code run, and test quickly. Both input + output in the same terminal!

core Article's
30 articles in total
Favicon
Understanding Process Management in Operating Systems
Favicon
Introduction to Operating Systems
Favicon
What is SignalR? A Real-Time Communication Framework for .NET
Favicon
[KOSD] Change of FromQuery Model Binding from .NET 6 to .NET8
Favicon
JDK, JVM, and JRE: The Three Musketeers of Java Development πŸ‡
Favicon
Optimizing Your Development Machine: How Many Cores and Threads Do You Need for Programming?
Favicon
ASP .NET Core Best Practices
Favicon
Build Scalable Blazor CRUD Apps in 30 Minutes with Azure SQL
Favicon
The Benefits of Having More Threads than Cores: Unlocking the Power of Multi-threading in Modern Computing
Favicon
Deep Dive ASP.NET Core Middleware : Part 1
Favicon
The Purpose of Computer Processors (CPUs) and How Multiple Cores Improve Speed and Performance
Favicon
Primitive Data Types in Java
Favicon
How I Build a Scratch Proxy Server Using Node.js
Favicon
Javascript Working Mechanism
Favicon
A Journey Into the World of Programming with First Core Java Program
Favicon
[KOSD] Learning from Issues: Troubleshooting Containerisation for .NET Worker Service
Favicon
CORE WEB VITAL ISSUE
Favicon
Migrate to TLS 1.2 for Azure Blob Storage
Favicon
Keyboard input in Node.js
Favicon
Criando sua primeira aplicação console em .net
Favicon
Enabling CORS in a .NET Core Server-Side Application
Favicon
Digital Image Processing Notes
Favicon
Top Benefits of Using Core App Dashboard
Favicon
Understanding JavaScript Execution Context β€” The Key to Efficient Code
Favicon
5 Interesting Things About Strings in Java
Favicon
πŸ”’ Introducing Serial Log and Metro Log: Simplifying Your Logging Experience In .NET MAUI! πŸ“πŸš‡
Favicon
Understanding Core.js and Zone.js: Key to JavaScript Libraries
Favicon
Apitable.com Net6 Rest Api
Favicon
Custom resolve handler in Microsoft's ServiceProvider (enabling property injection)
Favicon
ASP.NET CORE REACT APP: System.InvalidOperationException: SPA default page middleware failed to return default page /index.html

Featured ones: