Mousetrap - a simple library for handling keyboard shortcuts in Javascript

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>
时间: 2024-08-04 21:30:24

Mousetrap - a simple library for handling keyboard shortcuts in Javascript的相关文章

OS X: Keyboard shortcuts

Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a character key. For example, pressing the Command key (it has a ? symbol) and then the "c" key copies whatever is currently selected to the Clipboard.

Essential Windows Keyboard Shortcuts

Using keyboard shortcuts help you use your laptop without a mouse Keyboard shortcuts greatly enhance your productivity and save you a whole lot of time. Instead of pointing and clicking with the touchpad or external mouse, you can keep your hands on

[转]UiPath Keyboard Shortcuts

本文转自:https://docs.uipath.com/studio/docs/keyboard-shortcuts The complete list of keyboard shortcuts for UiPath Studio: File Management Ctrl + Shift + N - Creates a new Blank Process. Ctrl + O - Enables you to open a previously created workflow, eithe

A SIMPLE LIBRARY TO BUILD A DEEP ZOOM IMAGE

My current project requires a lot of work with Deep Zoom images. We recently received some very high-res photos, around 500M tiff files, some of which Deep Zoom Composer was unable to process. My first thought was to split them into smaller pieces an

【HDOJ】1497 Simple Library Management System

链表. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXM 1001 6 #define MAXN 100001 7 8 int un[MAXM], ub[MAXM]; 9 int v[MAXN]; 10 int next[MAXN]; 11 12 int comp(const void *a, const void *b) { 13 return *(int *)

SQL Server Management Studio Keyboard shortcuts

一些平时在SQL Server Management Studio 使用到的快捷键 F5 (Ctrl+x)执行选中部分的语句,没有选中则全文执行 Ctrl+L 现实执行计划(估计) Ctrl+M 在运行后后显示执行计划(实际) Shift+Alt+S 现实统计信息(实际) Ctrl+J 手动智能提示 Tab,Shift+Tab 选中语句进行增加或者减少缩进 CTRL+SHIFT+U,CTRL+SHIFT+L 进行语句的大小写调整.注意WHERE字句中的条件字符也会被改写 By MSDN:SQL

在Mac OSX下设置前端开发环境

以下内容将会详细介绍本人在MAC OSX下关于前端开发环境的安装和设置,如果您是前端开发人员,并且手上有个全新的或者重新安装过系统的mac,你可能会在以下内容里发现你所需要的东西. Google Chrome 几乎是每个前端开发者必备的浏览器,下载地址: https://www.google.com/chrome 一些常用到得Chrome插件: [JSON Formatter(显示格式化过得JSON文件)](https://chrome.google.com/webstore/detail/js

Eclipse Shortcuts - Tutorial

Eclipse Shortcuts - Tutorial Lars Vogel (c) 2009, 2017 vogella GmbHVersion 2.8,25.04.2017 Table of Contents 1. Shortcuts 2. Quick Access 3. Navigation and text selection 4. Start Java programs 5. Editing in the Java editor 6. Coding 7. Refactoring 8.

Terminator Shortcuts

Terminator is a useful tool for arranging terminals. It has following features Arrange terminals in a grid Tabs Drag and drop re-ordering of terminals Lots of keyboard shortcuts Save multiple layouts and profiles via GUI preferences editor Simultaneo