Git Product home page Git Product logo

shortcut.js's Introduction

shortcut.js Code Climate Circle CI

Documentation for v1 is found here

Ever wanted to add keyboard shortcuts to your website, but couldnt justify the time to develop it?

Shortcut.js gives you easy access to add shortcuts to your already existing functions.

Features!

  • Chainable
  • Global and Individual pause/resume functions
  • Cross OS Key Bindings ('mod' key)
  • CrossBrowser (IE8+, Chrome, FF, Safari Tested)
  • UMD compatibility

Installation

bower install shortcut npm install shortcut.js

How to use

General
function uppercaseInput(e){...}
function validateInput(e){...}

shortcut('mod =', document.querySelector('input#uppercase'))
  .bindsTo(uppercaseInput)
  .bindsTo(validateInput);

Here, when a user presses the the modifier key with the equals key, we run an uppercaseInput function, and then a validateInput function.

API

shortcut(shortcutKeys, DOMNode)

  • String shortcutKeys: A space delimited set of keys to press. For special keys, use the following mappings: shift, meta, optn, ctrl, space, down, right, up, left, tab, rtn

  • HTMLElement DOMNode: The DOMNode you want the shortcuts to trigger on.

+pause()

This function globally pauses all shortcut function dispatching. Keyboard shortcuts pressed after this function will not fire their associated functions.

Useage:

shortcut.pause();

+resume()

This function globally resumes all shortcut function dispatching. Keyboard shortcuts pressed after this function WILL fire their associated functions.

Useage:

shortcut.resume();

-bindsTo(fn: Function)

  • Function fn: A function which gets ran when the shortcutKeys are pressed in the appropriate selector.

-preventDefault()

A function that prevents default on the current shortcut. The normal event.preventDefault() is also respected. Usage:

shortcut('meta a', input).bindsTo(uppercaseInput).preventDefault();

This usage would cause cmd + a to not hightlight the input, but rather call the uppercaseInput functions.

-stopPropagation()

A function that prevents bubbling on the current node to the document.body. The normal event.stopPropagation() is also respected. Usage:

shortcut('meta a', input).bindsTo(uppercaseInput).stopPropagation();
shortcut('meta a', document.body).bindsTo(grabText);

This usage would cause cmd + a call the uppercaseInput function, but not the grabText function.

-trigger()

A function to trigger the functions associated to a shortcut. Usage:

shortcut('meta a', input).bindsTo(
  e => console.log('triggerMe!')
).trigger();
// Triggers the bindsTo function and outputs:
// "triggerMe!"

-unbind()

A function used to remove bindings to keyboard shortcuts.

Usage:

shortcut('meta a', input).unbind();

Now when a user presses meta a in an input, it will not touch shortcut and run its normal native events.

-pause()

Pauses event dispatching for the associated shortcut.

Usage:

shortcut('meta a', input).pause();

When a user presss meta a in an input, it will not fire associated functions. Once -resume() is called they will fire.

-resume()

Resumes event dispatching for the associated shortcut.

Usage:

shortcut('meta a', input).pause();
// pressing meta a in an input does nothing
shortcut('meta a', input).resume();
// pressing meta a dispatches the functions again

When a user presss meta a in an input, it will not fire associated functions. Once -resume() is called they will fire.

License

Licensed under the MIT license. Copyright 2014-2015 Blaine Kasten

shortcut.js's People

Contributors

blainekasten avatar oliverandrich 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.