Mousetrap is a simple library for handling keyboard shortcuts in Javascript.

It is around 2kb minified and gzipped and 4.5kb minified, has no external dependencies, and has been tested in the following browsers:

  • Internet Explorer 6+
  • Safari
  • Firefox
  • Chrome

It has support for keypress, keydown, and keyup events on specific keys, keyboard combinations, or key sequences.

Getting started

Include mousetrap on your page before the closing </body> tag

<script src="/path/to/mousetrap.min.js"></script>

Add some keyboard events to listen for

<script>
// single keys
Mousetrap.bind('4', function() { console.log('4'); });
Mousetrap.bind("?", function() { console.log('show shortcuts!'); });
Mousetrap.bind('esc', function() { console.log('escape'); }, 'keyup'); // combinations
Mousetrap.bind('command+shift+k', function() { console.log('command shift k'); }); // map multiple combinations to the same callback
Mousetrap.bind(['command+k', 'ctrl+k'], function() {
console.log('command k or control k'); // return false to prevent default browser behavior
// and stop event from bubbling
return false;
}); // gmail style sequences
Mousetrap.bind('g i', function() { console.log('go to inbox'); });
Mousetrap.bind('* a', function() { console.log('select all'); }); // konami code!
Mousetrap.bind('up up down down left right left right b a enter', function() {
console.log('konami code');
});
</script>

Others

More: https://craig.is/killing/mice

Download: mousetrap-1.5.3.zip

Mousetrap - Keyboard shortcuts in Javascript的更多相关文章

  1. OS X: Keyboard shortcuts

    Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a char ...

  2. [No0000113]Keyboard shortcuts for Windows Visual Studio Code

    General 常用Ctrl+Shift+P, F1 Show Command Palette 显示命令行Ctrl+P Quick Open, Go to File… 快速打开Ctrl+Shift+N ...

  3. keyboard shortcuts & Xcode 10

    keyboard shortcuts & Xcode 10 Xcode Keyboard Shortcuts https://swifteducation.github.io/assets/p ...

  4. MBP 2018 & Mac keyboard shortcuts

    MBP 2018 & Mac keyboard shortcuts https://support.apple.com/en-us/HT201236 delete key === Contro ...

  5. [转]UiPath Keyboard Shortcuts

    本文转自:https://docs.uipath.com/studio/docs/keyboard-shortcuts The complete list of keyboard shortcuts ...

  6. Google Chrome Keyboard Shortcuts

    Navigation To do this Press this Open a new window Ctrl + N Open a new tab Ctrl + T Open a new windo ...

  7. [转]33 useful Keyboard Shortcuts for Run commond

    原文: http://www.shortcutworld.com/en/win/Run-command.html 1. Calling Run CommandWin + r               ...

  8. SQL Server Management Studio Keyboard shortcuts

    一些平时在SQL Server Management Studio 使用到的快捷键 F5 (Ctrl+x)执行选中部分的语句,没有选中则全文执行 Ctrl+L 现实执行计划(估计) Ctrl+M 在运 ...

  9. RStudio Keyboard Shortcuts

    Console Description Windows & Linux Mac Move cursor to Console Ctrl+2 Ctrl+2 Clear console Ctrl+ ...

随机推荐

  1. Hadoop伪分布配置与基于Eclipse开发环境搭建

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. pic/at89c2051 programmer

    http://dangerousprototypes.com/forum/viewtopic.php?t=170 It looks like the PICKIT2 uses a small boos ...

  3. The source attachment does not contain the source for the file SignatureParser.class错误

    在myeclipse整合tomcat的完毕后,再启动tomcat的时候会出现这样的错误,呵呵,错误的大致意思是什么相关联错误,其实是myeclipse新加入的tomcat的模式出现错误了,myecli ...

  4. python中使用list作为默认参数且调用时不给其赋值的问题

    最近在写代码时发现一个有趣的地方,当python中的函数使用list作为默认参数且调用时不给其赋值时,无法通过在函数中将其赋值为[]来达到清空此默认参数的目的.按照道理来说,函数f1中的list为局部 ...

  5. 【M9】利用destructors避免泄漏资源

    1.在堆上获取的动态资源,用户忘记delete,或者由于异常导致没有没执行到delete,都会造成资源泄漏. 2.我们知道,栈上的对象,离开作用域,必定要执行析构方法.即使抛出异常,会堆栈回滚,保证已 ...

  6. Swift学习笔记十四

    Deinitialization 当类的实例对象即将要被释放时,会立即调用deinitializer,通过deinit关键字来定义deinitializer,和initializer一样,它也只存在于 ...

  7. UVALive 4192 Close Enough Computations 水题

    Close Enough Computations 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge& ...

  8. Apache的Order Allow Deny心得

    Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是: O ...

  9. 如何实现一个c/s模式的flv视频点播系统

    一.写在前面 视频点播,是一个曾经很热,现如今依然很热的一项视频服务技术.本人最近致力于研究将各种视频格式应用于点播系统中,现已研究成功FLV, F4V, MP4, TS格式的视频点播解决方案,完全支 ...

  10. [CSS3 Animation] TweenMax.staggerTo()

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...