123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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,
- Enter: 13,
- Escape: 27
- };
|