@accessible/use-key 中文文档教程

发布于 4年前 浏览 17 项目主页 更新于 3年前


useKey()

捆绑恐惧症 代码覆盖率 构建状态 NPM 版本 麻省理工学院许可证

npm i @accessible/use-key

用于处理特定 event.key 值上的 keydown 事件的 React 挂钩。 它也是 将来自 IE 的非标准 event.key 值规范化为现代等效值。

Quick Start

```jsx和谐 从“反应”导入*作为反应 import useKey

从 '@accessible/use-key' const Component = () => { const ref = React.useRef(null)

// 监听上面 ref 的 keydown 事件 使用密钥(参考,{ // 当按下 Escape 键时记录事件 转义:console.log, // 按下 Enter 时将“Key was pressed: Enter”记录到控制台 输入:(事件)=> console.log('按键被按下:', event.key), })

// 监听窗口上的按键事件 使用键(窗口,{ // 当按下 Escape 键时记录事件 转义:console.log, // 按下 Enter 时将“Key was pressed: Enter”记录到控制台 输入:(事件)=> console.log('按键被按下:', event.key), })

返回

} ```

API

useKey(target, handlers)

Arguments

ArgumentTypeRequired?Description
targetReact.RefObject<T> | T | Window | Document | nullYesA React ref, element, window, or document to add the key listener to
handlersRecord<string, (event?: KeyboardEvent) => any>YesA mapping with keys matching the event.key string you want to listen on. The value for each key should be an event listener.

Returns void

LICENSE

麻省理工学院


useKey()

Bundlephobia Code coverage Build status NPM Version MIT License

npm i @accessible/use-key

A React hook for handling keydown events on specific event.key values. It also normalizes non-standard event.key values from IE to their modern equivalents.

Quick Start

```jsx harmony import * as React from 'react' import useKey from '@accessible/use-key'

const Component = () => { const ref = React.useRef(null)

// Listens to keydown events on the ref above useKey(ref, { // Logs event when the Escape key is pressed Escape: console.log, // Logs "Key was pressed: Enter" to the console when Enter is pressed Enter: (event) => console.log('Key was pressed:', event.key), })

// Listens to keydown events on the window useKey(window, { // Logs event when the Escape key is pressed Escape: console.log, // Logs "Key was pressed: Enter" to the console when Enter is pressed Enter: (event) => console.log('Key was pressed:', event.key), })

return

} ```

API

useKey(target, handlers)

Arguments

ArgumentTypeRequired?Description
targetReact.RefObject&lt;T&gt; &#124; T &#124; Window &#124; Document &#124; nullYesA React ref, element, window, or document to add the key listener to
handlersRecord<string, (event?: KeyboardEvent) => any>YesA mapping with keys matching the event.key string you want to listen on. The value for each key should be an event listener.

Returns void

LICENSE

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文