@fluentui/keyboard-key

中文文档
0.4.23发布于 2024/06/24MITTypeScriptESM + CJS近 30 天下载 109.5万

A simple utility for determining the KeyboardEvent.key property from a keyboard event.

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. getKey()
  4. getCode()
  5. Why?
  6. Locale Caveat

@fluentui/keyboard-key

A simple utility for determining the KeyboardEvent.key property from a keyboard event.

Install

yarn add keyboard-key

# or

npm install keyboard-key

Usage

getKey()

Get the key property value from an event.

document.addEventListener('keydown', event => {
  const key = keyboardKey.getKey(event);

  switch (key) {
    case 'Escape':
      // handle escape key
      break;
    default:
      break;
  }
});

See MDN or the source for a full list of key values.

getCode()

You can also get the normalized keyCode from an event. @fluentui/keyboard-key includes a hash of key names to keyCodes for easy comparisons:

document.addEventListener('keydown', event => {
  const code = getCode(event);

  switch (code) {
    case keyboardKey.Escape: // 27
      // handle escape key
      break;
    default:
      break;
  }
});

Why?

Most previous key identifying KeyboardEvent properties have been pressed have been deprecated in favor of Keyboard.key.

:-1: KeyboardEvent.char :-1: KeyboardEvent.charCode :-1: KeyboardEvent.keyCode :-1: KeyboardEvent.keyIdentifier :-1: KeyboardEvent.keyLocation :-1: KeyboardEvent.which

:+1: KeyboardEvent.key

Unfortunately, KeyboardEvent.key does not yet have full browser support. Leaving the browsers without a reliable property for identifying keyboard event keys.

Locale Caveat

This utility interprets use of the shift key when inferring event key values. Example, an event describing shift+/ would result in a key value of ?. This logic assumes an en-US locale keyboard layout. This will not work if you are using a different locale such as a German layout where / is shift+7.

版本与发布记录

@fluentui/keyboard-key 共发布 56 个版本,首次发布于 2020/03/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.23latest2024/06/242 年前
0.4.222024/06/062 年前
0.4.212024/05/282 年前
0.4.202024/05/242 年前
0.4.192024/05/232 年前
0.4.182024/05/202 年前

关于 @fluentui/keyboard-key 的常见问题

@fluentui/keyboard-key 是什么?

A simple utility for determining the KeyboardEvent.key property from a keyboard event.

如何安装 @fluentui/keyboard-key?

在项目目录运行 npm install @fluentui/keyboard-key,或使用 pnpm add / yarn add / bun add。

@fluentui/keyboard-key 支持 TypeScript 吗?

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

@fluentui/keyboard-key 支持 ESM 还是 CommonJS?

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

@fluentui/keyboard-key 可以商用吗?

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

@fluentui/keyboard-key 的下载量有多少?

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

@fluentui/keyboard-key 还在维护吗?

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