Created
August 18, 2015 11:32
-
-
Save BlackScorp/fc551ccf874a2219f25d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Keyboard = function(){ | |
this.keys = { | |
'BACKSPACE': 8, | |
'TAB': 9, | |
'ENTER': 13, | |
'PAUSE': 19, | |
'CAPS': 20, | |
'ESC': 27, | |
'SPACE': 32, | |
'PAGE_UP': 33, | |
'PAGE_DOWN': 34, | |
'END': 35, | |
'HOME': 36, | |
'LEFT_ARROW': 37, | |
'UP_ARROW': 38, | |
'RIGHT_ARROW': 39, | |
'DOWN_ARROW': 40, | |
'INSERT': 45, | |
'DELETE': 46, | |
'0': 48, | |
'1': 49, | |
'2': 50, | |
'3': 51, | |
'4': 52, | |
'5': 53, | |
'6': 54, | |
'7': 55, | |
'8': 56, | |
'9': 57, | |
'A': 65, | |
'B': 66, | |
'C': 67, | |
'D': 68, | |
'E': 69, | |
'F': 70, | |
'G': 71, | |
'H': 72, | |
'I': 73, | |
'J': 74, | |
'K': 75, | |
'L': 76, | |
'M': 77, | |
'N': 78, | |
'O': 79, | |
'P': 80, | |
'Q': 81, | |
'R': 82, | |
'S': 83, | |
'T': 84, | |
'U': 85, | |
'V': 86, | |
'W': 87, | |
'X': 88, | |
'Y': 89, | |
'Z': 90, | |
'NUMPAD_0': 96, | |
'NUMPAD_1': 97, | |
'NUMPAD_2': 98, | |
'NUMPAD_3': 99, | |
'NUMPAD_4': 100, | |
'NUMPAD_5': 101, | |
'NUMPAD_6': 102, | |
'NUMPAD_7': 103, | |
'NUMPAD_8': 104, | |
'NUMPAD_9': 105, | |
'MULTIPLY': 106, | |
'ADD': 107, | |
'SUBSTRACT': 109, | |
'DECIMAL': 110, | |
'DIVIDE': 111, | |
'F1': 112, | |
'F2': 113, | |
'F3': 114, | |
'F4': 115, | |
'F5': 116, | |
'F6': 117, | |
'F7': 118, | |
'F8': 119, | |
'F9': 120, | |
'F10': 121, | |
'F11': 122, | |
'F12': 123, | |
'SHIFT': 16, | |
'CTRL': 17, | |
'ALT': 18, | |
'PLUS': 187, | |
'COMMA': 188, | |
'MINUS': 189, | |
'PERIOD': 190 | |
}; | |
this.keyDown = {}; | |
this.anyDown = false; | |
this.countKeys = 0; | |
this.active = true; | |
} | |
Keyboard.prototype.disable = function(){ | |
this.active = false; | |
} | |
Keyboard.prototype.enable = function(){ | |
this.active = true; | |
} | |
Keyboard.prototype.enabled = function(){ | |
return this.active; | |
} | |
Keyboard.prototype.isDown = function(key){ | |
if(!this.active) return false; | |
if(!key) return this.anyDown; | |
if(typeof key === "string") key = this.keys[key]; | |
return !!this.keyDown[key]; | |
} | |
Keyboard.prototype.dispatch = function(e){ | |
var code = null; | |
if (!e) var e = window.event; | |
if (e.keyCode) code = e.keyCode; | |
else if (e.which) code = e.which; | |
else if (e.code) code = e.code; | |
if(e.type === "keydown"){ | |
if(!this.keyDown[code]){ | |
this.keyDown[code] = true; | |
this.countKeys++; | |
} | |
} | |
if(e.type === "keyup"){ | |
if(this.keyDown[code]){ | |
delete this.keyDown[code]; | |
this.countKeys--; | |
} | |
} | |
if(this.countKeys > 0){ | |
this.anyDown = true; | |
}else{ | |
this.anyDown = false; | |
} | |
} | |
Keyboard.prototype.preventDefault = function(e){ | |
if (!e) var e = window.event; | |
e.cancelBubble = true; | |
if (e.stopPropagation) e.stopPropagation(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment