@danharrin/alpine-mousetrap

中文文档
0.1.11发布于 2026/11/07MITES Module零依赖近 30 天下载 2万

A simple library for handling keyboard shortcuts with Alpine.js.

原始使用文档
文档目录9 节
  1. About
  2. Installation
  3. CDN
  4. NPM
  5. Usage
  6. Global bindings
  7. Slash (/)
  8. Versioning
  9. License

✨ Help support the maintenance of this package by sponsoring me.

Alpine.js Mousetrap

A simple library for handling keyboard shortcuts with Alpine.js.

GitHub tag (latest by date) Monthly downloads via CDN

About

This plugin adds a new x-mousetrap directive to all of your Alpine components that can be used to register keyboard shortcuts.

Installation

CDN

Include the following <script> tag in the <head> of your document, just before Alpine.

<script src="https://cdn.jsdelivr.net/npm/@danharrin/[email protected]/dist/alpine-mousetrap.js" defer></script>

NPM

npm install @danharrin/alpine-mousetrap --save-dev

Add the x-mousetrap directive to your project by importing the package before Alpine.js.

import Alpine from 'alpinejs'
import Mousetrap from '@danharrin/alpine-mousetrap'

Alpine.plugin(Mousetrap)

window.Alpine = Alpine
window.Alpine.start()

Usage

To register keyboard shortcuts for a button or other clickable element:

<div x-data="{}">
    <button
        type="button"
        x-mousetrap.command-k.ctrl-k
    >
        Button
    </button>
</div>

Check out the original Mousetrap documentation for more shortcut examples.

If the shortcut uses a +, replace it with a -. If the shortcut is a sequence, replace the space with --. If you're registering more than one shortcut, separate them with a period.

Alternatively, you can invoke a custom function when the keyboard shortcut is hit:

<div x-data="{}">
    <button
        type="button"
        x-mousetrap.command-k.ctrl-k="console.log('command k or control k')"
    >
        Button
    </button>
</div>

Global bindings

You may listen for "global" keyboard events that will work anywhere, including inside input fields:

<div x-data="{}">
    <button
        type="button"
        x-mousetrap.global.command-k.ctrl-k
    >
        Button
    </button>
</div>

Slash (/)

You may listen for "/" keypress events using the keyword "slash":

<div x-data="{}">
    <button
        type="button"
        x-mousetrap.global.slash
    >
        Button
    </button>
</div>

Versioning

This projects follow the Semantic Versioning guidelines.

License

Copyright (c) Dan Harrin

Licensed under the MIT license, see LICENSE.md for details.

版本与发布记录

@danharrin/alpine-mousetrap 共发布 12 个版本,首次发布于 2022/05/05;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.1.11latest2026/11/073 天前
0.1.102026/08/302 个月前
0.1.92025/11/2811 个月前
0.1.82025/10/041.1 年前
0.1.72025/03/201.6 年前
0.1.62024/05/152 年前

关于 @danharrin/alpine-mousetrap 的常见问题

@danharrin/alpine-mousetrap 是什么?

A simple library for handling keyboard shortcuts with Alpine.js.

如何安装 @danharrin/alpine-mousetrap?

在项目目录运行 npm install @danharrin/alpine-mousetrap,或使用 pnpm add / yarn add / bun add。

@danharrin/alpine-mousetrap 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/danharrin__alpine-mousetrap 是否存在。

@danharrin/alpine-mousetrap 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@danharrin/alpine-mousetrap 可以商用吗?

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

@danharrin/alpine-mousetrap 的下载量有多少?

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

@danharrin/alpine-mousetrap 还在维护吗?

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