@bigbinary/neeto-hotkeys

中文文档
1.0.9发布于 2024/11/22ISCTypeScriptESM + CJS零依赖近 30 天下载 3.8万

React hook for handling keyboard shortcuts, offering a customizable and platform-aware solution, supporting global and scoped modes.

原始使用文档
文档目录4 节
  1. Installation
  2. Arguments that the useHotKeys hook accepts:
  3. Return value:
  4. Usage:

@bigbinary/neeto-hotkeys

The neeto-hotkeys package provides the useHotKeys hook, a versatile utility for managing hotkeys in an application. This hook allows you to define specific hotkey combinations and associate them with corresponding handler functions. The associated handler is invoked upon pressing the configured hotkey(s), enabling you to execute actions in response to keyboard input.

Installation

yarn add @bigbinary/neeto-hotkeys

Arguments that the useHotKeys hook accepts:

  • hotkey: A string specifying the hotkey(s) to listen for. Hotkeys can be defined in three formats: sequential, simultaneous & single.

    1. Sequential: Any keys separated by a space are considered sequential. The handler is invoked when the user presses the keys in sequence. For example, s r means that when the user presses s followed by an r the corresponding handler will be invoked.
    2. Simultaneous: Simultaneous hotkeys require all specified keys to be pressed at the same time to trigger the handler. For example, command+shift+r means that when command, shift, r are pressed at the same time the handler will be invoked.
    3. Single: As the name indicates, this denotes single keys like r , k, return etc. Whenever r is pressed its corresponding handler will be called.

    The hotkey should be in MacOS format, the hook will take care of converting it to the users platform(eg: command -> ctrl for windows).

    If we want to bind multiple hotkeys to the same handler we can pass in an array like so useHotkeys(['a', 'b'], handler).

  • handler: The function that should be invoked when the hotkey is pressed.

    Handler will receive the original key event. This can be used to stop the default browser action like so event.preventDefault().

  • config: A config object which has 3 properties mode, unbindOnUnmount & enabled.

    1. mode: The available values for mode are default, global & scoped.
      • default: It is the default mode. Handlers will only be called if the user is outside of a textarea, input, or select element.
      • global: Handlers will be fired even if the user is inside form elements like textarea.
      • scoped: It is used for scoping a hotkey to a DOM element. You can use this option if you want to invoke the handlers only if the user is focused in a specific div, button, textarea and so on. When this mode is set the hook will return a ref, you need to attached that ref to the element to which you need to scope the hotkey.
    2. unbindOnUnmount: By default its value will be true. If you don't want a handler to be unregistered when a component is unmounted then set the value to false.
    3. enabled: By default its value will be true. Setting this to false will not register the hotkey.
    4. document: This is an optional property. If you want to listen for hotkeys on an external document (e.g., an iframe), pass the reference of that document using the document property in the config object. If you do not provide this property, the hook will listen for hotkeys on the current document by default.

Return value:

  • inputRef: A ref which needs to be attached to the input element to be listened to.

Usage:

Following illustrates the usage of useHotKeys hook in implementing shortcut for Sidebar opening.

import useHotKeys from "@bigbinary/neeto-hotkeys";

// openSidebar function will only be called if the user is focused inside the textarea and performs the key combination.
const ref = useHotKeys("command+shift+r", openSidebar, {
  mode: "scoped",
});

return (
  <div>
    <div>Hello world</div>
    <textarea ref={ref}></textarea>
  </div>
);

Hotkeys are a fundamental aspect of many applications, enhancing user efficiency and interactivity. The useHotKeys hook simplifies the implementation of these hotkeys by allowing you to specify the hotkey combinations in various formats, associated handlers, and configuration options.

版本与发布记录

@bigbinary/neeto-hotkeys 共发布 9 个版本,首次发布于 2023/12/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.9latest2024/11/222 年前
1.0.82024/11/212 年前
1.0.72024/08/092 年前
1.0.62024/07/162 年前
1.0.52024/06/262 年前
1.0.42024/03/142 年前

关于 @bigbinary/neeto-hotkeys 的常见问题

@bigbinary/neeto-hotkeys 是什么?

React hook for handling keyboard shortcuts, offering a customizable and platform-aware solution, supporting global and scoped modes.

如何安装 @bigbinary/neeto-hotkeys?

在项目目录运行 npm install @bigbinary/neeto-hotkeys,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bigbinary/neeto-cist(latest)、@bigbinary/neeto-commons-frontend(latest)、ramda(^0.29.1)、react(^18.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bigbinary/neeto-hotkeys 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bigbinary/neeto-hotkeys 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bigbinary/neeto-hotkeys 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bigbinary/neeto-hotkeys 可以商用吗?

@bigbinary/neeto-hotkeys 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bigbinary/neeto-hotkeys 的下载量有多少?

根据 npm 官方下载统计,@bigbinary/neeto-hotkeys 在 2026/10/08 – 2026/11/06 这 30 天内被下载 37,561 次,日均约 1,252 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bigbinary/neeto-hotkeys 还在维护吗?

最新版本 1.0.9 发布于 2024/11/22(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/12/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。