開発環境 Visual Studio Community 2015
実行環境 Microsoft Edge


index.html
<!DOCTYPE html>
<head>
    <title>KeyInput</title>
    <script src="app.js"></script>
</head>
<body>
    <div id="content"></div>
</body>
 

app.ts
class Keyboard {
    keyDown: Array<boolean>;
 
    constructor() {
        this.keyDown = [];
        document.onkeydown = (e) => {
            this.keyDown[e.keyCode] = true;
        };
        document.onkeyup = (e) => {
            this.keyDown[e.keyCode] = false;
        };
    }
 
    IsKeyDown(keyCode: number) {
        return this.keyDown[keyCode];
    }
}
 
class Main {
    element: HTMLElement;
    keyboard: Keyboard;
    x: number;
    y: number;
 
    constructor(element: HTMLElement) {
        this.element = element;
        this.keyboard = new Keyboard();
        this.x = 0;
        this.y = 0;
    }
 
    start() {
        setInterval(() => {
            if (this.keyboard.IsKeyDown(37)) this.x--;  // Left
            if (this.keyboard.IsKeyDown(38)) this.y++;  // Up
            if (this.keyboard.IsKeyDown(39)) this.x++;  // Right
            if (this.keyboard.IsKeyDown(40)) this.y--;  // Down
            this.element.innerHTML = "x=" + this.x + " y=" + this.y;
        }, 1000 / 30);
    }
}
 
window.onload = () => {
    var el = document.getElementById("content");
    var main = new Main(el);
    main.start();
};
 

app.js
var Keyboard = (function () {
    function Keyboard() {
        var _this = this;
        this.keyDown = [];
        document.onkeydown = function (e) {
            _this.keyDown[e.keyCode] = true;
        };
        document.onkeyup = function (e) {
            _this.keyDown[e.keyCode] = false;
        };
    }
    Keyboard.prototype.IsKeyDown = function (keyCode) {
        return this.keyDown[keyCode];
    };
    return Keyboard;
})();
var Main = (function () {
    function Main(element) {
        this.element = element;
        this.keyboard = new Keyboard();
        this.x = 0;
        this.y = 0;
    }
    Main.prototype.start = function () {
        var _this = this;
        setInterval(function () {
            if (_this.keyboard.IsKeyDown(37))
                _this.x--; // Left
            if (_this.keyboard.IsKeyDown(38))
                _this.y++; // Up
            if (_this.keyboard.IsKeyDown(39))
                _this.x++; // Right
            if (_this.keyboard.IsKeyDown(40))
                _this.y--; // Down
            _this.element.innerHTML = "x=" + _this.x + " y=" + _this.y;
        }, 1000 / 30);
    };
    return Main;
})();
window.onload = function () {
    var el = document.getElementById("content");
    var main = new Main(el);
    main.start();
};
//# sourceMappingURL=app.js.map
 
最終更新:2015年08月25日 17:31