项目作者: medihack

项目描述 :
A modern keyboard event library for managing keyboard shortcuts.
高级语言: JavaScript
项目地址: git://github.com/medihack/keycuts.git
创建时间: 2018-05-16T14:25:11Z
项目社区:https://github.com/medihack/keycuts

开源协议:MIT License

下载


" class="reference-link">

keycuts is a modern Javascript library for handling keyboard shortcuts. It supports listening for key combinations, key sequences and a mix of both. keycuts has no external dependencies and a small footprint (~8kb, gzipped ~3kb).

Installation

Install with npm or Yarn.

  1. $ npm install keycuts --save
  2. # or
  3. $ yarn add keycuts

Alternatively you can download the distributed library and link it into your webpage.

  1. <script src="keycuts.min.js"></script>

Usage

  1. // Import using ES6 modules syntax
  2. import { Keys } from 'keycuts'
  3. // or when linked directly in the webpage
  4. const Keys = keycuts.Keys
  5. // Create a new object (without any parameter it is directly attached
  6. // to the window object)
  7. const keys = new Keys()
  8. // Trigger a provided callback when a specific key combination is pressed
  9. keys.bind(['Control', 'a'], () => {
  10. // triggered when Control and a are pressed
  11. })
  12. // You can also provide the shortcut using a string syntax
  13. keys.bind('Control + a', () => {
  14. // triggered when Control and a are pressed
  15. })
  16. // Sequences of key or key combinations are supported too
  17. key.bind([['Control', 'a'], ['Alt', 'b']], () => {
  18. // triggered when Control and a followed by Control and b are pressed
  19. })
  20. // And the appropriate string syntax
  21. key.bind('Control + a > Control + b', () => {
  22. // triggered when Control and a followed by Control and b are pressed
  23. })
  24. // Watch every keydown or keyup event and have access
  25. // to the current key sequence
  26. key.watch((event, sequence) => {
  27. // acces the currently pressed key combo
  28. const combo = sequence[sequence.length - 1]
  29. })

But keycuts can do more! The full documentation, API reference and a demo can be found on the keycuts homepage.

Development

For development fetch the code from Github and install the dependencies.

  1. $ git clone https://github.com/medihack/keycuts.git
  2. $ cd keycuts
  3. $ npm install # or yarn install

keycuts uses Webpack to transpile the source code (src/) into a minified and non-minified bundle in the dist/ folder.

  1. $ npm run build

You can easily start a development server with the demo which automatically recompiles the code and refreshes the page when the source files are modified (files under dist/ stay untouched).

  1. // Start a development server on port 8000
  2. $ npm run start
  3. // Just visit localhost:8000 in your browser

Run the test suite (using Jest) once or automatically when a source or test file was changed.

  1. // Run all tests once
  2. $ npm run test
  3. // or contiously
  4. $ npm run test:watch

More scripts to manage the library can be found in the scripts section in the (./package.json) file.

To contribute, please fork, add your patch, write tests for it and submit a pull request.

Contributions

Logo by Torsten Kühr (werkst.de)

License

MIT © Kai Schlamp