const bindAll = require('lodash.bindall');

class VMManager {
    constructor (vm) {
        bindAll(this, [
            'attachKeyboardEvents',
            'detachKeyboardEvents',
            'onKeyDown',
            'onKeyUp'
        ]);
        this.vm = vm;
    }
    attachKeyboardEvents () {
        // Feed keyboard events as VM I/O events.
        document.addEventListener('keydown', this.onKeyDown);
        document.addEventListener('keyup', this.onKeyUp);
    }
    detachKeyboardEvents () {
        document.removeEventListener('keydown', this.onKeyDown);
        document.removeEventListener('keyup', this.onKeyUp);
    }
    onKeyDown (e) {
        // Don't capture keys intended for Blockly inputs.
        if (e.target !== document && e.target !== document.body) return;

        // Don't capture browser keyboard shortcuts
        if (e.metaKey || e.altKey || e.ctrlKey) return;

        this.vm.postIOData('keyboard', {
            keyCode: e.keyCode,
            isDown: true
        });
        e.preventDefault();
    }
    onKeyUp (e) {
        // Always capture up events,
        // even those that have switched to other targets.
        this.vm.postIOData('keyboard', {
            keyCode: e.keyCode,
            isDown: false
        });

        // E.g., prevent scroll.
        if (e.target !== document && e.target !== document.body) {
            e.preventDefault();
        }
    }
}

module.exports = VMManager;

const bindAll = require('lodash.bindall');

class VMManager {
    constructor (vm) {
        bindAll(this, [
            'attachKeyboardEvents',
            'detachKeyboardEvents',
            'onKeyDown',
            'onKeyUp'
        ]);
        this.vm = vm;
    }
    attachKeyboardEvents () {
        // Feed keyboard events as VM I/O events.
        document.addEventListener('keydown', this.onKeyDown);
        document.addEventListener('keyup', this.onKeyUp);
    }
    detachKeyboardEvents () {
        document.removeEventListener('keydown', this.onKeyDown);
        document.removeEventListener('keyup', this.onKeyUp);
    }
    onKeyDown (e) {
        // Don't capture keys intended for Blockly inputs.
        if (e.target !== document && e.target !== document.body) return;

        this.vm.postIOData('keyboard', {
            keyCode: e.keyCode,
            isDown: true
        });

        // Don't stop browser keyboard shortcuts
        if (e.metaKey || e.altKey || e.ctrlKey) return;

        e.preventDefault();
    }
    onKeyUp (e) {
        // Always capture up events,
        // even those that have switched to other targets.
        this.vm.postIOData('keyboard', {
            keyCode: e.keyCode,
            isDown: false
        });

        // E.g., prevent scroll.
        if (e.target !== document && e.target !== document.body) {
            e.preventDefault();
        }
    }
}

module.exports = VMManager;

Send modifier keys to VM but allow bubbling
Send modifier keys to VM but allow bubbling

This allows the keys to be used for developer shortcuts, but also keeps the "if any key pressed" block working (and hacked key blocks).
