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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tinky-keypress

v1.3.0

Published

Keypress handling context and hooks for Tinky

Readme

tinky-keypress

English | 日本語

Tinky 应用提供强大的按键处理和输入解析功能。

本软件包提供了一套专用的 Context 和 Hooks 系统,用于管理终端用户界面中的标准输入 (stdin)。它处理了解析原始 ANSI 转义序列的复杂性,支持特殊键、修饰键以及现代终端协议。

功能特性

  • 易于集成:即插即用的 Context 提供者 (KeypressProvider) 和 Hook (useKeypress)。
  • 全面解析:支持标准键、功能键 (F1-F12)、导航键等。
  • 修饰键支持:支持 Control、Alt/Meta 和 Shift 修饰键。
  • 高级协议:可选支持 Kitty 键盘协议。
  • 鼠标与剪贴板:内置鼠标事件过滤和 OSC 52 粘贴序列支持。
  • 类型安全:使用 TypeScript 编写,提供完整的类型定义。

安装

npm install tinky-keypress
# 或
yarn add tinky-keypress
# 或
pnpm add tinky-keypress
# 或
bun add tinky-keypress

使用方法

使用 KeypressProvider 包裹应用根组件,并在任何子组件中使用 useKeypress

import React, { useState } from "react";
import { render, Text } from "tinky";
import { KeypressProvider, useKeypress } from "tinky-keypress";

function App() {
  const [lastEvent, setLastEvent] = useState<string>("请按任意键...");

  useKeypress(
    (key) => {
      const parts = [key.name];
      if (key.ctrl) parts.push("Ctrl");
      if (key.meta) parts.push("Alt");
      if (key.shift) parts.push("Shift");

      setLastEvent(
        `检测到: ${parts.join("+")} (序列: ${JSON.stringify(key.sequence)})`,
      );

      if (key.name === "q" && !key.ctrl) {
        process.exit(0);
      }
    },
    { isActive: true },
  );

  return <Text>{lastEvent}</Text>;
}

const instance = render(
  <KeypressProvider>
    <App />
  </KeypressProvider>,
);

API

<KeypressProvider />

监听 process.stdin 的顶层 Provider。

| 属性 | 类型 | 默认值 | 描述 | | --------------- | ----------- | ------- | ----------------------------------------- | | children | ReactNode | N/A | 可以使用按键 Hooks 的子组件。 | | kittyProtocol | boolean | false | 启用 Kitty 键盘协议支持(如果终端支持)。 |

useKeypress(handler, options)

订阅按键事件的 Hook。

useKeypress(
  handler: (key: Key, raw: string) => void,
  options: { isActive: boolean }
): void
  • handler: 当发生按键时调用的函数。
  • options.isActive: 是否启用监听器(可用于焦点管理)。

Key 接口

传递给处理程序的事件对象:

interface Key {
  name: string; // 按键名称 (例如 'a', 'enter', 'escape', 'up')
  ctrl: boolean; // 是否按下了 Control 键
  meta: boolean; // 是否按下了 Alt/Meta 键
  shift: boolean; // 是否按下了 Shift 键
  insertable: boolean; // 该按键是否产生可打印字符
  sequence: string; // 接收到的原始 ANSI 序列
}

许可证

Apache-2.0