12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- function KeyboardInput() {
- var keys = [];
- var press = [];
- var release = [];
- this.attach = function() {
- for(var i = 0; i < 256; i++) {
- keys[i] = false;
- press[i] = false;
- release[i] = false;
- }
- window.addEventListener("keydown", function(event) {
- if(!keys[event.keyCode]) {
- press[event.keyCode] = true;
- }
- keys[event.keyCode] = true;
- });
- window.addEventListener("keyup", function(event) {
- if(keys[event.keyCode]) {
- release[event.keyCode] = true;
- }
- keys[event.keyCode] = false;
- });
- }
- this.isPressed = function(code) {
- var pressed = press[code] && keys[code];
- press[code] = false;
- return pressed;
- }
- this.isReleased = function(code) {
- var released = release[code] && !keys[code];
- release[code] = false;
- return released;
- }
- this.isDown = function(code) {
- return keys[code];
- }
- };
- window.Keys = {
- Left: 37,
- Up: 38,
- Right: 39,
- Down: 40,
- W: 87,
- A: 65,
- S: 83,
- D: 68,
- Space: 32
- };
|