Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gugutz/shortway

A shortway to handle keyboard shortcuts.
https://github.com/gugutz/shortway

Last synced: 27 days ago
JSON representation

A shortway to handle keyboard shortcuts.

Awesome Lists containing this project

README

        

# Shortway
[![npm (scoped)](https://img.shields.io/npm/v/shortway.svg)](https://www.npmjs.com/package/shortway)
[![build status](https://img.shields.io/travis/gugutz/shortway.svg)](https://travis-ci.com/gugutz/shortway)
[![coverage status](https://img.shields.io/coveralls/github/gugutz/bulma-ui.svg?style=flat)](https://coveralls.io/github/gugutz/bulma-ui?branch=master)
[![dependencies status](https://img.shields.io/david/gugutz/shortway.svg?style=flat)](https://david-dm.org/gugutz/shortway)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)

A very small and dependency-free library to handle keyboard shortcuts in the browser. Feel free to use it with any kind of keyboard events and attach them to any element that you want.

[Try shortway live on codepen!](http://codepen.io/gugutz/full/YNEadb/)

## Install
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.

```sh
$ npm install --dev shortway
```

Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:

```javascript
// using ES6 modules
import shortway from 'shortway'

// using CommonJS modules
var shortway = require('shortway')
```

The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):

```html

```
You can find the library on `window.shortway`.

## Usage
```javascript
import shortway from 'shortway'

const callback = (e) => {console.log(e)}

const spaceShortcut = shortway('ctrl+space', callback)
document.addEventListener('keyup', spaceShortcut)

const customShortcut = shortway('ctrl+shift+left', callback)
document.querySelector('input').addEventListener('keypress', customShortcut)
```

## Supported Keys
Shortway current supports ctrl, alt and shift along with the following shortcuts:

- backspace
- tab
- enter
- esc
- space
- pageup
- pagedown
- end
- home
- 0-9
- a-z
- f1-f12
- Arrows: left, top, right, down
- insert
- delete
- slash: /

## API

### shortway(command, callback)

#### command
A sequence of keys to be watched.

##### Examples:
```javascript
shortway('r', callback)
shortway('ctrl+9', callback)
shortway('alt+a', callback)
shortway('shift+space', callback)
```

#### callback
A callback to be executed when a sequence of keys is predded.

##### Examples:
```javascript
const callback = e => {
console.log(e)
}

shortway('r', callback)
```

## Contributing
See the [contributing file](CONTRIBUTING.md).

## License
[MIT License](LICENSE.md) © [Gustavo P Borges](https://gugutz.github.io/)