KeyboardJS - "构建你的应用吧,我会处理按键"

简介: KeyboardJS  - "构建你的应用吧,我会处理按键"太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)本文遵循“署名-非商业用途-保持一致”创作公用协议转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。

KeyboardJS  - "构建你的应用吧,我会处理按键"

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。



KeyboardJS

"Build your app, I'll handle the keys."

Demo

command, windows, win, super, leftcommand, leftwindows, leftwin, leftsuper, c

What is KeyboardJS?

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts. It can be used as both a standalone library or an AMD module (see  RequireJS for details).

Get it Here

Language Support

KeyboardJS can support any locale, however out of the box it just comes with the US locale (for now). Adding a new locale is easy. Map your keyboard to an object and pass it to KeyboardJS.locale.register(localeName, localeDefinition) then call KeyboardJS.locale(localeName). 

If you create a new locale please consider sending me a pull request or submit it to the  issue tracker so I can add it to the library.

Methods

KeyboardJS.on

Usage

KeyboardJS.on(keyCombo, onDownCallback, onUpCallback);
Binds any key or key combo. See 'keyCombo' definition below for details. The onDownCallback is fired once the key or key combo becomes active. The onUpCallback is fired when the combo no longer active (a single key is released). 

Both the onUpCallback and the onUpCallback are passed three arguments. The first is the key event, the second is the keys pressed, and the third is the key combo string.

Returned

  • clear() - Removes the key or key combo binding.
  • on() - Allows you to bind to the keyup and keydown event of the given combo. An alternative to adding the onDownCallback and onUpCallback.

KeyboardJS.activeKeys

Usage

KeyboardJS.activeKeys();
Returns an array of active keys by name.

Returned

Returns an array of key names that are currently being pressed.

KeyboardJS.clear

Usage

KeyboardJS.clear(keyCombo);
Removes all bindings with the given key combo. See 'keyCombo' definition for more details. 

Please note that if you are just trying to remove a single binding should use the clear method in the object returned by KeyboardJS.on instead of this. This function is for removing all binding that use a certain key.

KeyboardJS.clear.key

Usage

KeyboardJS.clear.key(key);
Removes all bindings that use the given key.

KeyboardJS.locale

Usage

KeyboardJS.locale(localeName);
Changes the locale keyboardJS uses to map key presses. Out of the box KeyboardJS only supports US keyboards, however it is possible to add additional locales via KeyboardJS.locale.register()

KeyboardJS.locale.register

Usage

KeyboardJS.locale.register(localeName, localeDefinition);
Adds new locale definitions to KeyboardJS.

Definitions

keyCombo

A string containing key names separated by whitespace,  >+, and  ,.

examples

  • 'a' - binds to the 'a' key. Pressing 'a' will match this keyCombo.
  • 'a, b' - binds to the 'a' and 'b' keys. Pressing either of these keys will match this keyCombo.
  • 'a b' - binds to the 'a' and 'b' keys. Pressing either of these keys will match this keyCombo.
  • 'a + b' - binds to the 'a' and 'b' keys. Pressing both of these keys will match this keyCombo.
  • 'a > b' - binds to the 'a' and 'b' keys. Pressing 'a' then 'b' will match this keyCombo.
  • 'a + b, c + d' - binds to the 'a', 'b', 'c' and 'd' keys. Pressing either the 'a' key and the 'b' key, or the 'c' and the 'd' key will match this keyCombo.
  • 'a + b > c + d' - binds to the 'a', 'b', 'c' and 'd' keys. Pressing both the 'a' key and the 'b' key, then both the 'c' and the 'd' key will match this keyCombo.

localeDefinitions

An object that maps keyNames to their keycode and stores locale specific macros. Used for mapping keys on different locales.

examples

  • { "map": { "65": ["a"], "66": ["b"], ... }, "macros": [ ["shift + `", ["tilde", "~"]], ["shift + 1", ["exclamation", "!"]], .... ] }

Credits

I ( Robert Hurst) made this to enable better access to key controls in my applications. I'd like to share it with fellow devs. Feel free to fork this project and make your own changes.




目录
相关文章
|
编解码 JavaScript 前端开发
jsQR 一个完全独立的javascript 二维码识别库
jsQR 是一款纯粹的由javascript实现的二维码识别库,可以在浏览器端使用,也可以在后端node.js环境使用。我之前使用过其他的识别库,例如:qrcode-reader 或其他,在使用上都比较麻烦,而且识别率并不高。jsQR是后来发现的,感觉(没有实际对比验证)jsQR识别率要更高些,使用起来也更简单,不需要安装其他依赖软件。
jsQR 一个完全独立的javascript 二维码识别库
|
3月前
|
人工智能 机器人 API
国内值得关注的 AI 资讯网站推荐与每日追踪方法
AI资讯过载?本文精选7个国内优质平台(如RadarAI、机器之心、新智元等),覆盖技术深度、产业落地与开发者实操,并提供“1聚合+2垂直+每日15分钟”高效追踪法,助你快速抓重点、转机会。
3204 6
|
测试技术 开发工具 git
利用Git Hooks自动化你的开发流程
Git Hooks 是 Git 的强大功能,允许在特定事件(如提交、推送、合并请求)发生时自动执行脚本。通过合理设置和使用 Git Hooks,可以自动化测试、代码风格检查等任务,提高代码质量和开发效率。本文介绍了如何设置和使用常用的 Git Hooks,包括 `pre-commit`、`pre-push` 和 `post-merge`,并提供了最佳实践建议。
|
JavaScript 数据可视化 Docker
简易制作MCP服务器并测试
本文介绍了如何简易制作并测试MCP服务器,包括环境搭建、代码实现及Docker部署。首先通过uv包创建项目,在main.py中定义MCP服务器及其工具和资源函数。接着详细说明了在Windows上安装uv、配置Docker镜像加速、生成requirements.txt文件以及编写Dockerfile的过程。最后,通过构建和运行Docker容器部署MCP服务器,并使用Node.js工具测试其功能,确保服务器正常工作。此教程适合初学者快速上手MCP服务器的开发与部署。
4731 63
|
监控 API 开发工具
Socket.IO介绍,以及怎么连接测试Socket.IO接口?
Socket.IO 是一个用于浏览器和服务器间实时双向通信的库,支持低延迟消息传递、跨平台运行及自动重连。文章介绍了其特点与调试需求,并详细说明如何使用 Apifox 工具创建、连接、发送/接收 Socket.IO 事件,以及团队协作和调试技巧。掌握这些技能可提升实时应用开发效率与质量。
|
JSON 监控 JavaScript
Swagger UI 本地主机教程: 如何在本地使用 Swagger UI?
Swagger UI 提供在线和离线版本,但由于各种原因,你可能需要在本地使用 Swagger UI。 在本文中,我们将向你展示如何在本地使用 Swagger UI。
|
Ubuntu 芯片 开发者
Ubuntu 25 ARM 桌面系统抢先版发布:第一个Ubuntu ARM桌面系统
Ubuntu 25.04 将于2025年发布,首次支持ARM Desktop桌面版系统,为ARM架构设备如Mac M系列芯片、Raspberry Pi等带来全新的桌面体验。用户可通过虚拟机或双系统安装在Mac上运行Ubuntu ARM,抢先体验版已开放下载:[链接](https://www.baihezi.com/ubuntu/arm/desktop)。此版本不仅扩展了Ubuntu的硬件兼容性,还提供了丰富的功能和流畅的操作体验,适合开发者和技术爱好者尝试。
6165 9
|
机器学习/深度学习 Linux 开发者
Python必备工具:pip的安装与管理
Python必备工具:pip的安装与管理
2236 0
|
存储 机器学习/深度学习 人工智能
【AI大模型】Transformers大模型库(十六):safetensors存储类型
【AI大模型】Transformers大模型库(十六):safetensors存储类型
2830 0
|
负载均衡 监控 JavaScript
后台服务守护进程神器pm2介绍及使用
后台服务守护进程神器pm2介绍及使用