npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tty-events

v1.0.0

Published

A package for handling terminal input (mouse, keyboard, clipboard and focus).

Downloads

2,213

Readme

tty-events

tty-events is a package for handling events from the terminal, made for interactive, terminal-based applications.

Features

  • Full mouse and highlight tracking support (VT200 and SGR extended);
  • Bracketed paste mode support;
  • Focus support;
  • Full UTF-8 support;
  • Support for more key combinations (especially in Windows) when compared to node:readline’s keypress event;
  • Easy comparison (e.g. key == "Ctrl+s");
  • unknownSequence event.

Usage Examples

Keyboard

This script logs all key presses:

if (process.stdin.isTTY)
	process.stdin.setRawMode(true);

const term = new (require("tty-events"));

term.on("keypress", (key)=>{
	console.log("You pressed %s.", key.toString());
	if (key == "Ctrl+c") {
		term.pause(); // Exit the program
	}
});

Mouse

tty-events supports mouse (VT200 and SGR extended). In order to receive mouse events, the enableMouse() function must be called first.

term.enableMouse();

term.on("mousedown", (ev)=>{
	console.log("You clicked at (%i, %i) with the button no. %i.", ev.x, ev.y, ev.button);
});

For a highlight tracking example, see the example highlight tracking script.

Pasting

tty-events supports bracketed paste mode. This feature allows to distinguish between real keystrokes and pasted text from the clipboard. This is useful in applications where certain keys trigger some command. In order to receive paste events, the enableBPM() function must be called first.

term.enableBPM();

term.on("paste", (text)=>{
	console.log("You pasted %O.", text);
});

Focus

Focus events allow an application to stop updating the screen when it's not necessary. In order to receive focus events, the enableFocus() function must be called first.

term.enableFocus();

term.on("focusin", ()=>{
	console.log("The terminal received focus.");
});
term.on("focusout", ()=>{
	console.log("The terminal lost focus.");
});

Important Notes and Limitations

Because of the way terminals work, there are some aspects that might seem unintuitive.

Uppercase VS Shift

An uppercase letter emits an event with the uppercase letter and with the shift property set to false. This is because there is no way to know if Shift was being pressed (an uppercase letter can be produced with Caps Lock). For example: Shift+Alt+A emits Alt+A instead of Alt+Shift+a.

“Twin” Keys

Some key combinations produce the same output to stdin. Here is a list of the key combinations that may not work as expected:

  • Ctrl+Shift+<letter>: Emits Ctrl+<letter> (in lowercase). (The Shift modifier is ignored.)
  • Ctrl+H: Emits backspace. (Terminals send \b when Ctrl+H is pressed.)
  • Ctrl+I: Emits tab. (Terminals send \t when Ctrl+I is pressed.)
  • Ctrl+J: Emits enter. (Terminals send \n when Ctrl+J is pressed.)
  • Ctrl+M: Emits enter. (Terminals send \r when Ctrl+M is pressed.)
  • Ctrl+[: Emits escape. (Terminals send \x1B when Ctrl+[ is pressed.)
  • Shift+F1: Emits f11 in some terminals.
  • Shift+F2: Emits f12 in some terminals.

Escape Key

Listening for Esc (escape) is discouraged. Terminals send \x1B when Esc is pressed, which is the first byte of escape sequences, and this makes the detection unreliable.

Incompatible Terminals

Also, some features and/or key combinations don't work in some terminal emulators:

  • Mouse support isn't available in Windows Console.
  • Bracketed paste mode isn't available in Windows Console.
  • Ctrl+@ doesn't work in Windows Console and in some keyboard layouts Ctrl+\, Ctrl+], Ctrl+^ and Ctrl+- don't work too.
  • Mouse highlight tracking works in few terminals, however xterm supports it.

API Documentation

The full documentation is available here.