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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | 2x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 542x 92x 6x 5x 5x 379x 379x 10x 10x 10x 55x 390x 390x 264x 126x 3x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 409x 409x 409x 57x 57x 352x 5x 5x 347x 409x 409x 409x 411x 347x 347x 207x 207x 207x 140x 98x 98x 4x 4x 4x 3x 3x 94x 94x 2x 2x 92x 92x 92x 128x 128x 128x 128x 122x 122x 122x 2x 2x 2x 2x 1x 1x 4x 4x 3x 97x 19x 19x 19x 3x 1x 1x 2x 1x 1x 1x 2x 2x | import { isUserRemapped, keyCodes } from "../keymap.js";
import { Typist } from "../typist.js";
const isMac = typeof window !== "undefined" && /^Mac/i.test(window.navigator?.platform || "");
/**
* @typedef {Object} KeyboardConfig
* @property {Object} processor - The processor instance
* @property {Function} inputEnabledFunction - A function to check if input is enabled
* @property {string} [keyLayout="physical"] - The keyboard layout
* @property {Debugger} dbgr - The debugger instance
*/
/**
* 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, keyLayout = "physical", dbgr } = config;
// Core components
this.processor = processor;
this.inputEnabledFunction = inputEnabledFunction;
this.shortcutsBlockedFunction = shortcutsBlockedFunction ?? (() => false);
this.dbgr = dbgr;
this.keyInterface = processor.keyboardInterface;
this.typist = new Typist(processor);
// State
this.emuKeyHandlers = {};
this.running = false;
this.pauseEmu = false;
this.stepEmuWhenPaused = false;
this.keyLayout = keyLayout;
this.saidCapsLockIsTapped = false;
this.releases = new Map();
}
/**
* 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.
* @param {KeyboardEvent} evt - The keyboard event
* @returns {string}
*/
_machineKey(evt) {
if (this.keyLayout !== "natural") return evt.code;
// A `KEY.` parameter names a key by where it is, so it outranks what the key prints.
if (isUserRemapped(evt.code)) return evt.code;
// The Master's keypad is a separate set of keys from the digits above the letters, and
// the characters cannot tell them apart.
Iif (evt.code?.startsWith("Numpad")) return evt.code;
return evt.key?.length === 1 ? evt.key : evt.code;
}
/**
* Registers a handler for a specific key with optional modifiers
* @param {string} keyCode - The host key, by physical position
* @param {Function} handler - Called as (down, code, shiftKey) on the way down and up
* @param {Object} [options] - Options for this handler
* @param {boolean} [options.alt=true] - Whether this handler requires the Alt key
* @param {boolean} [options.ctrl=false] - Whether this handler requires the Ctrl key
*/
registerKeyHandler(keyCode, handler, options = { alt: true, ctrl: false }) {
// Generate a unique key that includes modifiers
const handlerKey = `${options.alt ? "alt:" : ""}${options.ctrl ? "ctrl:" : ""}${keyCode}`;
this.emuKeyHandlers[handlerKey] = {
handler,
alt: !!options.alt,
ctrl: !!options.ctrl,
keyCode,
};
}
/**
* 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);
}
/**
* Sets the running state of the emulator
* @param {boolean} isRunning - Whether the emulator is running
*/
setRunning(isRunning) {
this.running = isRunning;
}
/**
* Find a matching key handler for the given key event
* @param {string} keyCode - The host key, by physical position
* @param {boolean} altKey - Whether Alt is pressed
* @param {boolean} ctrlKey - Whether Ctrl is pressed
* @returns {Object|null} The handler object or null if none found
* @private
*/
_findKeyHandler(keyCode, altKey, ctrlKey) {
// Try to find a handler with exact modifier match first
const exactModKey = `${altKey ? "alt:" : ""}${ctrlKey ? "ctrl:" : ""}${keyCode}`;
if (this.emuKeyHandlers[exactModKey]) {
return this.emuKeyHandlers[exactModKey];
}
return null;
}
/**
* Handles a key press event
* @param {KeyboardEvent} evt - The keyboard event
*/
keyPress(evt) {
// Early returns for common scenarios
// Check if input is enabled. If inputEnabledFunction returns true, keyboard events should not be processed.
if (this.inputEnabledFunction()) return;
Iif (this.running || (!this.dbgr.enabled() && !this.pauseEmu)) return;
// The debugger's keys are the characters they print, not positions.
const key = evt.key;
// Handle debugger 'g' key press
if (this.dbgr.enabled() && key === "g") {
this.dbgr.hide();
this.dispatchEvent(new Event("resume"));
return;
}
// Handle pause/step control keys
Eif (this.pauseEmu) {
if (key === "g") {
this.resumeEmulation();
return;
} else Eif (key === "n") {
this.requestStep();
this.dispatchEvent(new Event("resume"));
return;
}
}
// Pass any other keys to the debugger if it's enabled
if (this.dbgr.enabled()) {
const handled = this.dbgr.keyPress(key);
if (handled) evt.preventDefault();
}
}
/**
* Handles a key down event
* @param {KeyboardEvent} evt - The keyboard event
*/
keyDown(evt) {
const code = this.keyCode(evt);
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) {
return this.shortcutsBlockedFunction() ? null : this._findKeyHandler(code, evt.altKey, evt.ctrlKey);
}
/** @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.handler(true, code, evt.shiftKey);
return () => handler.handler(false, code);
}
if (this.inputEnabledFunction() || !this.running) return null;
evt.preventDefault();
if (code === keyCodes.F12 || code === keyCodes.BREAK) {
this.dispatchEvent(new CustomEvent("break", { detail: true }));
this.processor.setReset(true);
return () => {
this.dispatchEvent(new CustomEvent("break", { detail: false }));
this.processor.setReset(false);
};
}
Iif (isMac && code === keyCodes.CAPSLOCK) {
this.handleMacCapsLock();
return null;
}
if (this.isPasting) {
if (code === keyCodes.ESCAPE) this.cancelPaste();
return null;
}
const machineKey = this._machineKey(evt);
this.keyInterface.keyDown(machineKey, evt.shiftKey);
return () => this.keyInterface.keyUp(machineKey);
}
/**
* Handles a key up event
* @param {KeyboardEvent} evt - The keyboard event
*/
keyUp(evt) {
const code = this.keyCode(evt);
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.keyInterface.keyDown(keyCodes.CAPSLOCK);
// Simulate a key release after a short delay
setTimeout(() => this.keyInterface.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();
this.keyInterface.clearKeys();
}
/**
* Called after each frame to determine if emulation should pause
* @returns {boolean} - True if emulation should pause
*/
postFrameShouldPause() {
if (this.stepEmuWhenPaused) {
this.stepEmuWhenPaused = false;
return true;
}
return false;
}
/**
* Request a single step of the emulator
*/
requestStep() {
this.stepEmuWhenPaused = true;
}
/**
* Pause the emulator
*/
pauseEmulation() {
this.pauseEmu = true;
this.dispatchEvent(new Event("pause"));
}
/**
* Resume the emulator
*/
resumeEmulation() {
this.pauseEmu = false;
this.dispatchEvent(new Event("resume"));
}
}
|