Giter VIP home page Giter VIP logo

js-keystroke's Introduction

JS keyStroke

A jQuery plugin and Underscore mixin for binding actions to keyboard combinations.

Usage

$.keyStroke() and _.keyStroke() take three arguments: requriedKeys, options, and callback. The options argument is optional. The last argument you pass will always be treated as the callback, so to exclude the optional argument you can just pass two.

// No options passed
$.keyStroke( [68, 70], function(){ console.log( 'Hello World' ); });

// Options included
$.keyStroke( 83, { modKeys: ['shiftKey'] }, function(){ console.log( 'Hello dog' ); });

Required

  • requiredKeys: Array of JavaScript keyCodes for your keystroke. Can be an integer (rather than an array) if you only want to use one keyCode, not including modifier keys passed in the options argument. Order is not important, and ordered keystrokes are not supported. This page is helpful for finding keyCodes.
  • callback: A function to call when your keystroke is executed.

Options

  • args: An array of arguments to be passed to your callback when your keystroke is exectuted. The last keydown's event object is always passed as the first argument to callback. Note: this has nothing to do with JavaScript's arguments object.
  • context: The value of this for your callback.
  • preventDefault: Whether or not to preventDefault() on the keydown event that triggers your keystroke. Defaults to true.
  • modKeys: Array of strings that match keydown event properties and can be used to include modifier keys in your keystroke. The modKeys option is only used if requiredKeys is a single (non-array) value, otherwise it is ignored. Examples: 'altKey', 'ctrlKey', 'metaKey', 'shiftKey'

Options should be passed via an object, e.g. {args: ['foo', 'bar'], context: someValueForThis, modKeys: ['altKey']}

Example

saveSomething = function( event ) {
	// ...do something with the keydown event that triggered the keystroke
	someSaveAction();
}

// Save something on ctrl + s
// s = 83
$.keyStroke( 83, { modKeys: [ 'ctrlKey' ] }, saveSomething );

// Do the same thing with Underscore instead of jQuery
_.keyStroke( 83, { modKeys: [ 'ctrlKey' ] }, saveSomething );

You can also use anonymous callbacks.

$.keyStroke( 83, { modKeys: [ 'altKey' ] }, function() { 'You pressed ALT + S!'; } );

See http://evansolomon.github.com/js-keystroke/ for an example of altering the DOM with a keystroke.

Installation

Load jquery.keystroke.js after jQuery, or _.keystroke.js after Underscore. Both plugins come with a matching min.js version.

js-keystroke's People

Contributors

evansolomon avatar

Stargazers

pah arif avatar Tony Wu avatar Arnstein Henriksen avatar David Hua avatar David Smith avatar Japh avatar

Watchers

Japh avatar  avatar Arnstein Henriksen avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.