All files / src/web keyboard.js

95.62% Statements 131/137
91.56% Branches 76/83
90.62% Functions 29/32
95.65% Lines 110/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305      12x 12x 12x                     12x 1218x 1218x     1412x                                       133x               133x     133x 133x 133x 133x 133x   133x 133x 133x 133x     133x 133x 133x 133x 133x         136x 188x 109x     4x 70x                   11x 11x 11x 11x     11x                   1220x                         188x   184x 144x 6x                 707x                 52x 52x 52x               802x 802x 802x 802x 29x 29x   773x 7x 7x       766x 802x 802x 802x       804x 537x           766x 766x 505x 505x 505x     261x 199x   199x 3x 3x     196x         196x 2x 2x     194x 188x 188x 188x   24x 24x 24x 5x 5x                   1218x 1218x 2436x   1218x 1218x 4360x 4360x 6x 6x                 416x 416x 416x       416x 416x 409x 409x 409x             2x       2x     2x   2x 1x 1x                             7x 7x       13x       201x             74x 74x   74x      
import { isUserRemapped, keyCodes } from "../keymap.js";
import { Typist } from "../typist.js";
 
export const isMac = typeof window !== "undefined" && /^Mac/i.test(window.navigator?.platform || "");
const InputMethodKeyCode = 229;
const KeypadNavigationKeys = new Set([
    keyCodes.END,
    keyCodes.HOME,
    keyCodes.PAGEUP,
    keyCodes.PAGEDOWN,
    keyCodes.INSERT,
    keyCodes.LEFT,
    keyCodes.UP,
    keyCodes.RIGHT,
    keyCodes.DOWN,
]);
const Modifiers = [
    { codes: [keyCodes.SHIFT_LEFT, keyCodes.SHIFT_RIGHT], isDown: (evt) => evt.shiftKey },
    { codes: [keyCodes.CTRL_LEFT, keyCodes.CTRL_RIGHT], isDown: (evt) => evt.ctrlKey },
];
 
const isComposing = (evt) => evt.key === "Process" || evt.keyCode === InputMethodKeyCode;
 
/**
 * @typedef {Object} KeyboardConfig
 * @property {Object} processor - The processor instance
 * @property {Function} inputEnabledFunction - A function to check if input is enabled
 * @property {Function} runningFunction - Whether the machine is running, and so takes keys
 * @property {Function} [resetInputMethodFunction] - Drops a dead key the host's input method is holding
 * @property {string} [keyLayout="physical"] - The keyboard layout
 */
 
/**
 * Keyboard class that handles all keyboard related functionality
 */
export class Keyboard extends EventTarget {
    /**
     * Create a new Keyboard instance with specified configuration
     * @param {KeyboardConfig} config - The configuration object
     */
    constructor(config) {
        super();
        const {
            processor,
            inputEnabledFunction,
            shortcutsBlockedFunction,
            runningFunction,
            resetInputMethodFunction = () => {},
            keyLayout = "physical",
        } = config;
 
        // Core components
        this.processor = processor;
        this.inputEnabledFunction = inputEnabledFunction;
        this.shortcutsBlockedFunction = shortcutsBlockedFunction ?? (() => false);
        this.runningFunction = runningFunction;
        this.resetInputMethodFunction = resetInputMethodFunction;
 
        this.keyInterface = processor.keyboardInterface;
        this.machineTypist = new Typist(processor);
        this.typist = this.machineTypist;
        this.input = this.directInput();
 
        // State
        this.altKeyHandlers = new Map();
        this.keyLayout = keyLayout;
        this.saidCapsLockIsTapped = false;
        this.releases = new Map();
        this.heldDeadKeys = new Set();
    }
 
    /** Key presses and BREAK straight to the machine. */
    directInput() {
        return {
            keyDown: (...args) => this.keyInterface.keyDown(...args),
            keyUp: (key) => this.keyInterface.keyUp(key),
            keyRaw: (colRow, down) =>
                down ? this.keyInterface.keyDownRaw(colRow) : this.keyInterface.keyUpRaw(colRow),
            setReset: (resetting) => this.processor.setReset(resetting),
            clearKeys: () => this.keyInterface.clearKeys(),
        };
    }
 
    /**
     * Routes key presses, pastes and BREAK through `input`, or straight to the machine again with
     * null, which starts it with nothing held: a session's keys go with the session. A paste is typed
     * through `input.keyRaw(colRow, down)`.
     */
    setInput(input) {
        this.cancelPaste();
        this.clearKeys();
        this.input = input ?? this.directInput();
        this.typist = input
            ? new Typist(this.processor, { input, isRunning: this.runningFunction })
            : this.machineTypist;
        this.input.clearKeys();
    }
 
    /**
     * The host key a keyboard event came from, by physical position. jsbeeb's own shortcuts
     * and special keys are always by position, whatever layout the machine is using.
     * @param {KeyboardEvent} evt - The keyboard event
     * @returns {string} - A `KeyboardEvent.code` name
     */
    keyCode(evt) {
        return evt.code;
    }
 
    /**
     * How the emulated machine's key map names the key this event came from. The natural
     * layout is keyed by the character the host produced, so that what you type comes out
     * right whatever layout the host keyboard is in; every other layout is by position. A
     * keypad key with Num Lock off is, in every layout, the navigation key it stands in for.
     * @param {KeyboardEvent} evt - The keyboard event
     * @returns {string}
     */
    _machineKey(evt) {
        // A `KEY.` parameter names a key by where it is, so it outranks what the key prints.
        if (isUserRemapped(evt.code)) return evt.code;
        // Even in the natural layout, a keypad key is the Master's keypad, not the character it types.
        if (evt.code?.startsWith("Numpad")) return KeypadNavigationKeys.has(evt.key) ? evt.key : evt.code;
        if (this.keyLayout !== "natural") return evt.code;
        return evt.key?.length === 1 ? evt.key : evt.code;
    }
 
    /**
     * Registers a handler for Alt and a key, held without Ctrl.
     * @param {string} keyCode - The host key, by physical position
     * @param {Function} handler - Called as (down, code, shiftKey) on the way down and up
     */
    registerAltKeyHandler(keyCode, handler) {
        this.altKeyHandlers.set(keyCode, handler);
    }
 
    /**
     * Updates the current key layout
     * @param {string} layout - The keyboard layout to use
     */
    setKeyLayout(layout) {
        // Anything still held was named by the old layout, so release it before the names change.
        this.clearKeys();
        this.keyLayout = layout;
        this.processor.setKeyLayout(layout);
    }
 
    /**
     * Handles a key down event
     * @param {KeyboardEvent} evt - The keyboard event
     */
    keyDown(evt) {
        const code = this.keyCode(evt);
        this._releaseKeysShownUp(evt, code);
        const stale = this.releases.get(code);
        if (evt.repeat && this._shortcutFor(evt, code)) {
            evt.preventDefault();
            return;
        }
        if (evt.repeat && stale) {
            if (!this.inputEnabledFunction()) evt.preventDefault();
            return;
        }
        // A fresh press of a key still held means its key up was lost, as macOS does for a key
        // released while Cmd is down.
        stale?.();
        this.releases.delete(code);
        const release = this._press(evt, code);
        if (release) this.releases.set(code, release);
    }
 
    _shortcutFor(evt, code) {
        if (!evt.altKey || evt.ctrlKey || this.shortcutsBlockedFunction()) return null;
        return this.altKeyHandlers.get(code) ?? null;
    }
 
    /** @returns {(() => void)|null} how to undo the press, or null if there is nothing to undo */
    _press(evt, code) {
        // Shortcuts come first, so they work while the machine is stopped and from inside the media window.
        const handler = this._shortcutFor(evt, code);
        if (handler) {
            evt.preventDefault();
            handler(true, code, evt.shiftKey);
            return () => handler(false, code);
        }
 
        if (this.inputEnabledFunction() || !this.runningFunction()) return null;
        evt.preventDefault();
 
        if (code === keyCodes.F12 || code === keyCodes.BREAK) {
            this.input.setReset(true);
            return () => this.input.setReset(false);
        }
 
        Iif (isMac && code === keyCodes.CAPSLOCK) {
            this.handleMacCapsLock();
            return null;
        }
 
        if (this.isPasting) {
            if (code === keyCodes.ESCAPE) this.cancelPaste();
            return null;
        }
 
        if (isComposing(evt)) return null;
        const machineKey = this._machineKey(evt);
        this.input.keyDown(machineKey, evt.shiftKey);
        if (evt.key !== "Dead") return () => this.input.keyUp(machineKey);
        // Stops the host's input method composing the next key with this one, as "Process" with no key up.
        this.resetInputMethodFunction();
        this.heldDeadKeys.add(code);
        return () => {
            this.heldDeadKeys.delete(code);
            this.input.keyUp(machineKey);
        };
    }
 
    /**
     * Lets go of held keys whose key up was lost, when this event shows they are up: a modifier
     * the event says is not held, and, once the input method composes the next key (which is
     * what Linux sends in place of a dead key's key up), any dead key.
     */
    _releaseKeysShownUp(evt, code) {
        const up = [];
        for (const { codes, isDown } of Modifiers) {
            if (!isDown(evt) && !codes.includes(code)) up.push(...codes);
        }
        if (isComposing(evt)) up.push(...this.heldDeadKeys);
        for (const heldCode of up) {
            const release = this.releases.get(heldCode);
            if (!release) continue;
            this.releases.delete(heldCode);
            release();
        }
    }
 
    /**
     * Handles a key up event
     * @param {KeyboardEvent} evt - The keyboard event
     */
    keyUp(evt) {
        const code = this.keyCode(evt);
        this._releaseKeysShownUp(evt, code);
        Iif (isMac && code === keyCodes.CAPSLOCK) {
            this._press(evt, code);
            return;
        }
        const release = this.releases.get(code);
        if (!release) return;
        this.releases.delete(code);
        evt.preventDefault();
        release();
    }
 
    /**
     * Special handling for Mac's Caps Lock key behavior
     */
    handleMacCapsLock() {
        const CAPS_LOCK_DELAY = 100;
 
        // Mac browsers seem to model caps lock as a physical key that's down when capslock is on, and up when it's off.
        // No event is generated when it is physically released on the keyboard. So, we simulate a "tap" here.
        this.input.keyDown(keyCodes.CAPSLOCK);
 
        // Simulate a key release after a short delay
        setTimeout(() => this.input.keyUp(keyCodes.CAPSLOCK), CAPS_LOCK_DELAY);
 
        if (this.saidCapsLockIsTapped) return;
        this.saidCapsLockIsTapped = true;
        this.dispatchEvent(
            new CustomEvent("notice", {
                detail: {
                    message:
                        "macOS sends no key up for caps lock, so jsbeeb can only tap it. " +
                        "For a game that holds caps lock for left or fire, remap that key instead.",
                    title: "Keyboard",
                    quietKey: "warnedAboutRubbishMacs",
                },
            }),
        );
    }
 
    /** Sends raw keys, and millisecond delays, to the machine: paste and autoboot come through here. */
    sendRawKeyboard(keysToSend, checkCapsAndShiftLocks) {
        this.clearKeys();
        this.typist.type(keysToSend, checkCapsAndShiftLocks);
    }
 
    cancelPaste() {
        this.typist.cancel();
    }
 
    get isPasting() {
        return this.typist.isTyping;
    }
 
    /**
     * Clears all pressed keys
     */
    clearKeys() {
        for (const release of this.releases.values()) release();
        this.releases.clear();
        // A session's machine is everyone's: only this person's keys are theirs to let go.
        this.input.clearKeys();
    }
}