Code Monkey home page Code Monkey logo

key-js's Introduction

key-js

Simple library for JavaScript key code constants and a utility to check key press state. It is a port of my KeyAS3 library.

Many existing libraries expose key code values in strange ways (such as requiring function calls that took string values). This library provides a simple static object containing the key codes, and comes with an additional utility function for checking the pressed state of keys. The API is heavily influenced by the Key class from the olden days of ActionScript 2.0.

Usage

CommonJS

var KeyJS = require('key-js');

<script> Tag

When loaded through a <script> tag in the browser, KeyJS will become a global variable on the window object.

Retrieving Key Code Constants

The KeyJS object exposes the key code values as values attached to the object itself. See below:

console.log(KeyJS.A); // 65
console.log(KeyJS.ENTER); // 13
console.log(KeyJS.SPACEBAR); // 32

See key.js for a full list of all the constants

Checking the state of the Keyboard

By default KeyJS will not listen for Keyboard events. You can use the function KeyJS.startCapture() to start the listeners. The full API can then be accessed as outlined below:

KeyJS.startCapture(target = null) - Starts listening for keyboard events on the target DOM element. By default this will be set to the window object. Note that keyboard events are only tracked while the target has focus. This means you don't have to worry about key presses becoming "stuck" in the pressed state when focus is lost.

KeyJS.endCapture() - Stops listening for keyboard events.

KeyJS.isDown(keyCode) - Checks the "pressed" state of the specified key specified by its key code.

Example Code

var KeyJS = require('key-js');
KeyJS.startCapture();

setInterval(function () {
  // Check console to see when spacebar is pressed
  console.log('Spacebar pressed: ' + KeyJS.isDown(KeyJS.SPACEBAR));
}, 100);

key-js's People

Contributors

cleod9 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

bowbahdoe

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.