1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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) {
- var keyCode = event.which || event.keyCode;
- /*if(null != document.getElementById("chat")) {
- var span = document.createElement("span");
- var text = document.createTextNode(keyCode);
- span.appendChild(text);
- document.getElementById("chat-output").appendChild(span);
- }*/
- if(!keys[keyCode]) {
- press[keyCode] = true;
- }
- keys[keyCode] = true;
- if(keyCode == Keys.Alt || keyCode == Keys.F1) {
- event.preventDefault();
- }
- });
- window.addEventListener("keyup", function(event) {
- var keyCode = event.which || event.keyCode;
- if(keys[keyCode]) {
- release[keyCode] = true;
- }
- keys[keyCode] = false;
- if(keyCode == Keys.Alt || keyCode == Keys.F1) {
- event.preventDefault();
- }
- });
- }
- 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];
- }
- this.clearBuffer = function() {
- for(var i = 0; i < 256; i++) {
- press[i] = false;
- }
- }
- };
- window.Keys = {
- Left: 37,
- Up: 38,
- Right: 39,
- Down: 40,
- W: 87,
- A: 65,
- S: 83,
- D: 68,
- I: 73,
- E: 69,
- F: 70,
- M: 77,
- T: 84,
- Space: 32,
- Enter: 13,
- Escape: 27,
- Esc: 27,
- Alt: 18,
- Shift: 16,
- Ctrl: 17,
- F1: 112
- };
|