jquery监听热键 hotkeys

jQuery Hotkeys 是一个插件,允许开发者轻松地在代码中添加和移除键盘事件处理程序,支持几乎所有的按键组合。该插件适用于 jQuery 1.4.2 及以上版本,在所有主流浏览器上均可正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

About

 

jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting almost any key combination.

 

This plugin is based off of the plugin by Tzury Bar Yochay: jQuery.hotkeys

 

The syntax is as follows:

$(expression).bind(types, keys, handler);
$(expression).unbind(types, handler);

$(document).bind('keydown', 'ctrl+a', fn);

// e.g. replace '$' sign with 'EUR'
$('input.foo').bind('keyup', '$', function(){
  this.value = this.value.replace('$', 'EUR');
});

 

Syntax when wanting to use jQuery's on()/off methods:

$(expression).on(types, null, keys, handler);
$(expression).off(types, handler);

$(document).on('keydown', null, 'ctrl+a', fn);

// e.g. replace '$' sign with 'EUR'
$('input.foo').on('keyup', null, '$', function(){
  this.value = this.value.replace('$', 'EUR');
});

 

Types

 

Supported types are 'keydown', 'keyup' and 'keypress'

 

Notes

 

If you want to use more than one modifiers (e.g. alt+ctrl+z) you should define them by an alphabetical order e.g. alt+ctrl+shift

 

Hotkeys aren't tracked if you're inside of an input element (unless you explicitly bind the hotkey directly to the input). This helps to avoid conflict with normal user typing.

 

jQuery Compatibility

 

Works with jQuery 1.4.2 and newer.

 

It known to be working with all the major browsers on all available platforms (Win/Mac/Linux)

 

  • IE 6/7/8
  • FF 1.5/2/3
  • Opera-9
  • Safari-3
  • Chrome-0.2

 

Addendum

 

Firefox is the most liberal one in the manner of letting you capture all short-cuts even those that are built-in in the browser such as Ctrl-t for new tab, or Ctrl-a for selecting all text. You can always bubble them up to the browser by returning true in your handler.

 

Others, (IE) either let you handle built-in short-cuts, but will add their functionality after your code has executed. Or (Opera/Safari) will not pass those events to the DOM at all.

 

So, if you bind Ctrl-Q or Alt-F4 and your Safari/Opera window is closed don't be surprised.

 

来源:https://github.com/jeresig/jquery.hotkeys

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值