Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

jullierme/angular-keycode

Repository files navigation

Bower Join the chat at https://gitter.im/jullierme/angular-keycode

No More Magic Numbers \o/

A code service of keyboard events

Less than 2 kb minified

Installation

bower

# To install latest release
bower install angular-keycode


# To install latest release and update bower.json
bower install angular-keycode --save

Please note that Angular KeyCode requires Angular 1.2.x or higher.

Registrer

angular.module('myApp',['angular-keycode']);

Import

<script src="bower_components/angular-keycode/angular-keycode.min.js"></script>

Using

(function(){
	'use strict';

	MyService.$inject = ['KeyCode'];

	function MyService(KeyCode){
		var vm = this;
		vm.onKeydown = onKeydown;

        //wrong
		function onKeydown($event){
			var code = $event.which || $event.keyCode;

			if(code === 13){//magic number detected
				...do
			}
		}

        //correct
		function onKeydown($event){
            var code = $event.which || $event.keyCode;

            if(code === KeyCode.ENTER){
                ...do
            }
        }

	}
})();

Examples

KeyCode.BACKSPACE
KeyCode.TAB
KeyCode.ENTER

KeyCode.A
KeyCode.B
KeyCode.C

KeyCode.NUMPAD_8
KeyCode.NUMPAD_9

KeyCode.F1
KeyCode.F2
KeyCode.F3

MORE...

LICENCE

MIT