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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | 138x 138x 138x 138x 138x 138x 138x 13110x 138x 25x 2404x 2404x 2358x 4716x 4716x 463x 463x 327x 221x 178x 178x 78x 78x 6x 138x 138x 18x 18x 18x 18x 79x 79x 79x 21x 21x 79x 4x 4x 79x 18x 18x 18x 13x 138x 138x 17x 7x 1012x 636x 376x 376x 376x 376x 188x 138x 1012x 1012x 1012x 202606x 202606x 103661x 103661x 98945x 98945x 4716x 1012x 6x 7x 7x 1012x 26312x 1012x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 4x 384x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 904x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 902x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 908x 968x 968x 968x 968x 975x 975x 975x 66x 66x 66x 66x 66x 66x 66x 66x 66x 36x 66x 53x 196x 5x 160x 160x 160x 160x 71x 138x 138x 138x 275x 15264x 954x 954x 954x 954x 194x 3104x 2400x 150x 150x 96x 6x 6x 1x 1x 1x 1x 24x 24x 24x 30x 29x 29x 14x 5x 5x 48x 48x 7x 7x 2x 2x 92x 92x 10x 91x | import { runningInNode } from "./loader.js";
export const userKeymap = [];
export const BBC = {
SEMICOLON_PLUS: [7, 5],
MINUS: [7, 1],
LEFT_SQUARE_BRACKET: [8, 3],
RIGHT_SQUARE_BRACKET: [8, 5],
COMMA: [6, 6],
PERIOD: [7, 6],
SLASH: [8, 6],
SHIFTLOCK: [0, 5],
TAB: [0, 6],
RETURN: [9, 4],
DELETE: [9, 5],
COPY: [9, 6],
SHIFT: [0, 0],
ESCAPE: [0, 7],
CTRL: [1, 0],
CAPSLOCK: [0, 4],
LEFT: [9, 1],
UP: [9, 3],
RIGHT: [9, 7],
DOWN: [9, 2],
K0: [7, 2],
K1: [0, 3],
K2: [1, 3],
K3: [1, 1],
K4: [2, 1],
K5: [3, 1],
K6: [4, 3],
K7: [4, 2],
K8: [5, 1],
K9: [6, 2],
Q: [0, 1],
W: [1, 2],
E: [2, 2],
R: [3, 3],
T: [3, 2],
Y: [4, 4],
U: [5, 3],
I: [5, 2],
O: [6, 3],
P: [7, 3],
A: [1, 4],
S: [1, 5],
D: [2, 3],
F: [3, 4],
G: [3, 5],
H: [4, 5],
J: [5, 4],
K: [6, 4],
L: [6, 5],
Z: [1, 6],
X: [2, 4],
C: [2, 5],
V: [3, 6],
B: [4, 6],
N: [5, 5],
M: [5, 6],
F0: [0, 2],
F1: [1, 7],
F2: [2, 7],
F3: [3, 7],
F4: [4, 1],
F5: [4, 7],
F6: [5, 7],
F7: [6, 1],
F8: [6, 7],
F9: [7, 7],
SPACE: [2, 6],
UNDERSCORE_POUND: [8, 2],
AT: [7, 4],
COLON_STAR: [8, 4],
PIPE_BACKSLASH: [8, 7],
HAT_TILDE: [8, 1],
// row 1
NUMPADPLUS: [10, 3],
NUMPADMINUS: [11, 3],
NUMPADSLASH: [10, 4],
NUMPADASTERISK: [11, 5],
// row 2
NUMPAD7: [11, 1],
NUMPAD8: [10, 2],
NUMPAD9: [11, 2],
NUMPADHASH: [10, 5],
// row 3
NUMPAD4: [10, 7],
NUMPAD5: [11, 7],
NUMPAD6: [10, 1],
NUMPAD_DELETE: [11, 4],
//row4
NUMPAD1: [11, 6],
NUMPAD2: [12, 7],
NUMPAD3: [12, 6],
NUMPADCOMMA: [12, 5],
//row 5
NUMPAD0: [10, 6],
NUMPAD_DECIMAL_POINT: [12, 4],
NUMPADENTER: [12, 3],
};
/**
* The key each gamepad button presses until the URL remaps it, by its index in the browser's standard mapping: A (0),
* Start (9) and the D-pad's up, down, left and right (12 to 15). The D-pad presses the "Snapper" keys.
*/
export const BbcGamepadKeys = {
0: BBC.RETURN,
9: BBC.SPACE,
12: BBC.COLON_STAR,
13: BBC.SLASH,
14: BBC.Z,
15: BBC.X,
};
const IsLetter = /^[A-Za-z]$/;
/** BBC keys that print the same thing either way, so holding one must not disturb shift. */
const ShiftMakesNoDifference = new Set([BBC.SPACE]);
/** The characters a natural layout tries to bind, of which each machine takes those it can print. */
export const PrintableCharacters = (() => {
const chars = ["\u00a3"];
for (let code = 32; code < 127; ++code) chars.push(String.fromCharCode(code));
return chars;
})();
/**
* How a natural layout binds a host key, a character or a position, to the key that makes `char`.
* The machine holds shift for a different set of characters than a PC does, so a key says which
* shift state it needs, as a third element, when that differs from the one being held.
*
* @param {Function} map binds (hostKey, colRow, shiftDown)
* @param {Function} keyForCharacter as `bbcKeyForCharacter`
* @param {Set} keysShiftLeavesAlone keys that print the same thing either way
* @returns {Function} binds (hostKey, char)
*/
export function characterBinder(map, keyForCharacter, keysShiftLeavesAlone) {
return (hostKey, char) => {
const needs = keyForCharacter(char);
if (!needs) return;
for (const shiftDown of [false, true]) {
// Overriding when the two already agree would take shift away from everything
// else held at the same time, and letters want the shift key as it is anyway.
const disagrees = needs.shift !== shiftDown && !IsLetter.test(char) && !keysShiftLeavesAlone.has(needs.key);
map(hostKey, disagrees ? [...needs.key, needs.shift] : needs.key, shiftDown);
}
};
}
/**
* How a BBC keyboard types a character: which key, and whether shift is held while it is
* pressed. `!` is shift and `1`; `^` is its own unshifted key. Lower case is the same key as
* upper with the caps lock the other way, which `upperCase` says.
*
* The one table for this; the natural keyboard layout and pasting both read it.
*
* @param {string} char one character
* @returns {{key: [number, number], shift: boolean, upperCase: boolean}|null} null if the BBC has no such character
*/
export function bbcKeyForCharacter(char) {
const code = char.charCodeAt(0);
if (code >= 65 && code <= 90) return { key: BBC[char], shift: false, upperCase: true };
if (code >= 97 && code <= 122) return { key: BBC[String.fromCharCode(code - 32)], shift: false, upperCase: false };
if (code >= 48 && code <= 57) return { key: BBC["K" + char], shift: false, upperCase: true };
const shifted = ShiftedCharacters[char];
if (shifted) return { key: shifted, shift: true, upperCase: true };
const unshifted = UnshiftedCharacters[char];
if (unshifted) return { key: unshifted, shift: false, upperCase: true };
return null;
}
/** Characters the BBC prints with shift held. `!` to `)` are shift and the digit above them. */
const ShiftedCharacters = {
"!": BBC.K1,
'"': BBC.K2,
"#": BBC.K3,
$: BBC.K4,
"%": BBC.K5,
"&": BBC.K6,
"'": BBC.K7,
"(": BBC.K8,
")": BBC.K9,
"=": BBC.MINUS,
"~": BBC.HAT_TILDE,
"|": BBC.PIPE_BACKSLASH,
"{": BBC.LEFT_SQUARE_BRACKET,
"+": BBC.SEMICOLON_PLUS,
"*": BBC.COLON_STAR,
"}": BBC.RIGHT_SQUARE_BRACKET,
"<": BBC.COMMA,
">": BBC.PERIOD,
"?": BBC.SLASH,
// Character 96, where ASCII has a backtick: the MOS font at &C000 draws a pound sign.
"\u00a3": BBC.UNDERSCORE_POUND,
};
/** Characters the BBC prints without shift. */
const UnshiftedCharacters = {
"\n": BBC.RETURN,
"\t": BBC.TAB,
" ": BBC.SPACE,
"-": BBC.MINUS,
"^": BBC.HAT_TILDE,
"\\": BBC.PIPE_BACKSLASH,
"@": BBC.AT,
"[": BBC.LEFT_SQUARE_BRACKET,
_: BBC.UNDERSCORE_POUND,
";": BBC.SEMICOLON_PLUS,
":": BBC.COLON_STAR,
"]": BBC.RIGHT_SQUARE_BRACKET,
",": BBC.COMMA,
".": BBC.PERIOD,
"/": BBC.SLASH,
};
/**
* The keys to press, in order, to type `str`, toggling `shiftKey` and `lockKey` as each character needs them and
* leaving both as they were found.
* @param {string} str
* @param {function(string): ?{key: number[], shift: boolean, upperCase?: boolean}} keyForCharacter where
* `upperCase` is left out for a character the lock does not change
* @param {number[]} shiftKey
* @param {number[]} lockKey
*/
export function stringToKeys(str, keyForCharacter, shiftKey, lockKey) {
const array = [];
let shiftState = false;
let capsLockState = true;
for (const char of str) {
const needs = keyForCharacter(char);
Iif (!needs) continue;
if (needs.shift !== shiftState) {
array.push(shiftKey);
shiftState = needs.shift;
}
if (needs.upperCase !== undefined && needs.upperCase !== capsLockState) {
array.push(lockKey);
capsLockState = needs.upperCase;
}
array.push(needs.key);
}
if (shiftState) array.push(shiftKey);
if (!capsLockState) array.push(lockKey);
return array;
}
export function stringToBBCKeys(str) {
return stringToKeys(str, bbcKeyForCharacter, BBC.SHIFT, BBC.CAPSLOCK);
}
/**
* Host keys by physical position, as `KeyboardEvent.code` names them:
* https://www.w3.org/TR/uievents-code/
*
* The names on the left are jsbeeb's own, and the `KEY.` URL parameters use them.
*/
export const keyCodes = {
SEMICOLON: "Semicolon",
APOSTROPHE: "Quote",
MUTE: "AudioVolumeMute",
MINUS: "Minus",
EQUALS: "Equal",
BACK_QUOTE: "Backquote",
BACKSPACE: "Backspace",
TAB: "Tab",
ENTER: "Enter",
BREAK: "Pause",
CAPSLOCK: "CapsLock",
ESCAPE: "Escape",
SPACE: "Space",
PAGEUP: "PageUp",
PAGEDOWN: "PageDown",
END: "End",
HOME: "Home",
LEFT: "ArrowLeft",
UP: "ArrowUp",
RIGHT: "ArrowRight",
DOWN: "ArrowDown",
PRINTSCREEN: "PrintScreen",
INSERT: "Insert",
DELETE: "Delete",
K0: "Digit0",
K1: "Digit1",
K2: "Digit2",
K3: "Digit3",
K4: "Digit4",
K5: "Digit5",
K6: "Digit6",
K7: "Digit7",
K8: "Digit8",
K9: "Digit9",
A: "KeyA",
B: "KeyB",
C: "KeyC",
D: "KeyD",
E: "KeyE",
F: "KeyF",
G: "KeyG",
H: "KeyH",
I: "KeyI",
J: "KeyJ",
K: "KeyK",
L: "KeyL",
M: "KeyM",
N: "KeyN",
O: "KeyO",
P: "KeyP",
Q: "KeyQ",
R: "KeyR",
S: "KeyS",
T: "KeyT",
U: "KeyU",
V: "KeyV",
W: "KeyW",
X: "KeyX",
Y: "KeyY",
Z: "KeyZ",
/* also COMMAND on Mac */
WINDOWS: "MetaLeft",
WINDOWS_RIGHT: "MetaRight",
MENU: "ContextMenu",
NUMPAD0: "Numpad0",
NUMPAD1: "Numpad1",
NUMPAD2: "Numpad2",
NUMPAD3: "Numpad3",
NUMPAD4: "Numpad4",
NUMPAD5: "Numpad5",
NUMPAD6: "Numpad6",
NUMPAD7: "Numpad7",
NUMPAD8: "Numpad8",
NUMPAD9: "Numpad9",
NUMPADASTERISK: "NumpadMultiply",
NUMPADPLUS: "NumpadAdd",
NUMPAD_DECIMAL_COMMA: "NumpadComma",
NUMPADMINUS: "NumpadSubtract",
NUMPAD_DECIMAL_POINT: "NumpadDecimal",
NUMPADSLASH: "NumpadDivide",
NUMPADENTER: "NumpadEnter",
F1: "F1",
F2: "F2",
F3: "F3",
F4: "F4",
F5: "F5",
F6: "F6",
F7: "F7",
F8: "F8",
F9: "F9",
F10: "F10",
F11: "F11",
F12: "F12",
NUMLOCK: "NumLock",
SCROLL_LOCK: "ScrollLock",
VOLUMEUP: "AudioVolumeUp",
VOLUMEDOWN: "AudioVolumeDown",
FASTFORWARD: "MediaTrackNext",
FASTREWIND: "MediaTrackPrevious",
PLAYPAUSE: "MediaPlayPause",
COMMA: "Comma",
PERIOD: "Period",
SLASH: "Slash",
LEFT_SQUARE_BRACKET: "BracketLeft",
RIGHT_SQUARE_BRACKET: "BracketRight",
BACKSLASH: "Backslash",
/* only on a 102-key board, between the left shift and the Z */
INTL_BACKSLASH: "IntlBackslash",
SHIFT_LEFT: "ShiftLeft",
SHIFT_RIGHT: "ShiftRight",
ALT_LEFT: "AltLeft",
ALT_RIGHT: "AltRight",
CTRL_LEFT: "ControlLeft",
CTRL_RIGHT: "ControlRight",
};
/**
* Other names a `KEY.` parameter may use for a host key: one that covers both sides of a pair,
* or a second name for a key that is printed differently on different keyboards.
*/
const keyCodeAliases = {
SHIFT: [keyCodes.SHIFT_LEFT, keyCodes.SHIFT_RIGHT],
CTRL: [keyCodes.CTRL_LEFT, keyCodes.CTRL_RIGHT],
ALT: [keyCodes.ALT_LEFT, keyCodes.ALT_RIGHT],
HASH: [keyCodes.BACKSLASH],
/* an Apple keyboard prints "clear" on the key a PC calls num lock */
CLEAR: [keyCodes.NUMLOCK],
};
/**
* The host keys a jsbeeb key name stands for, or an empty array if it names none.
* @param {string} name a `keyCodes` name, or an alias covering more than one
* @returns {string[]} `KeyboardEvent.code` names
*/
export function hostKeyCodes(name) {
if (keyCodes[name]) return [keyCodes[name]];
return keyCodeAliases[name] ?? [];
}
export function detectKeyboardLayout() {
if (runningInNode) {
return "UK";
}
Iif (localStorage.keyboardLayout) {
return localStorage.keyboardLayout === "US" ? "US" : "UK";
}
Eif (navigator.language) {
Iif (navigator.language.toLowerCase() === "en-gb") return "UK";
Eif (navigator.language.toLowerCase() === "en-us") return "US";
}
return "UK"; // Default guess of UK
}
/**
* Whether a `KEY.` parameter has claimed this host key. Those name a key by where it is, so they
* win over the character it prints in the layout that goes by character.
* @param {string} code a `KeyboardEvent.code` name
*/
export function isUserRemapped(code) {
return userKeymap.some((mapping) => hostKeyCodes(mapping.native).includes(code));
}
const Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* What both machines' key maps are built with, the letters already bound. `finish()` applies the `KEY.` URL
* parameters last, so they win, without consuming them: a map is rebuilt on layout and model changes, and the user's
* mapping has to survive that.
* @param {Object<string, number[]>} machineKeys the machine's keys by name, BBC or ATOM
*/
export function keyMapBuilder(machineKeys) {
const keys = [];
keys[true] = {};
keys[false] = {};
function bind(code, colRow, shiftDown) {
Iif (keys[shiftDown][code] && keys[shiftDown][code] !== colRow) {
console.log(
"Warning: duplicate binding for key",
(shiftDown ? "<SHIFT>" : "") + code,
colRow,
keys[shiftDown][code],
);
}
keys[shiftDown][code] = colRow;
}
function map(code, colRow, shiftDown) {
Iif (!code || !colRow) console.log("error binding key", code, colRow);
if (shiftDown === undefined) {
bind(code, colRow, true);
bind(code, colRow, false);
} else {
bind(code, colRow, shiftDown);
}
}
function finish() {
for (const mapping of userKeymap) {
for (const code of hostKeyCodes(mapping.native)) {
keys[true][code] = machineKeys[mapping.key];
keys[false][code] = machineKeys[mapping.key];
}
}
return keys;
}
for (const letter of Letters) map(keyCodes[letter], machineKeys[letter]);
return { map, finish };
}
export function getKeyMap(keyLayout) {
const isUKlayout = detectKeyboardLayout() === "UK";
const { map, finish } = keyMapBuilder(BBC);
map(keyCodes.F10, BBC.F0); // F0 (mapped to F10)
map(keyCodes.F1, BBC.F1);
map(keyCodes.F2, BBC.F2);
map(keyCodes.F3, BBC.F3);
map(keyCodes.F4, BBC.F4);
map(keyCodes.F5, BBC.F5);
map(keyCodes.F6, BBC.F6);
map(keyCodes.F7, BBC.F7);
map(keyCodes.F8, BBC.F8);
map(keyCodes.F9, BBC.F9);
// these keys are in the same place on PC and BBC keyboards
// including shifted characters
// so can be the same for "natural" and "gaming"
map(keyCodes.COMMA, BBC.COMMA);
map(keyCodes.PERIOD, BBC.PERIOD);
map(keyCodes.SLASH, BBC.SLASH);
map(keyCodes.SPACE, BBC.SPACE);
map(keyCodes.TAB, BBC.TAB);
map(keyCodes.ENTER, BBC.RETURN);
// see later map(keyCodes.SHIFT_LEFT, BBC.SHIFT_LEFT);
map(keyCodes.SHIFT_RIGHT, BBC.SHIFT);
// other keys to map to these in "game" layout too
map(keyCodes.LEFT, BBC.LEFT);
map(keyCodes.UP, BBC.UP);
map(keyCodes.RIGHT, BBC.RIGHT);
map(keyCodes.DOWN, BBC.DOWN);
if (keyLayout === "natural") {
// Keyed by the character the host keyboard produces, not by where a key sits, so the
// host's own layout never has to be guessed: press whatever gives a `@` and the BBC
// gets a `@`.
const mapCharacter = characterBinder(map, bbcKeyForCharacter, ShiftMakesNoDifference);
for (const char of PrintableCharacters) mapCharacter(char, char);
// The BBC has no backtick, and a US or Dvorak keyboard cannot type a pound sign at all,
// so the key that would print one gives the pound sign instead.
mapCharacter(keyCodes.BACK_QUOTE, "\u00a3");
// Keys that print nothing are still known by where they are.
map(keyCodes.SHIFT_LEFT, BBC.SHIFT);
map(keyCodes.END, BBC.COPY);
map(keyCodes.HOME, BBC.SHIFTLOCK);
map(keyCodes.F11, BBC.COPY);
map(keyCodes.ESCAPE, BBC.ESCAPE);
map(keyCodes.CTRL_LEFT, BBC.CTRL);
map(keyCodes.CTRL_RIGHT, BBC.CTRL);
map(keyCodes.CAPSLOCK, BBC.CAPSLOCK);
map(keyCodes.DELETE, BBC.DELETE);
map(keyCodes.BACKSPACE, BBC.DELETE);
} else if (keyLayout === "gaming") {
// gaming keyboard
// 1st row
map(keyCodes.ESCAPE, BBC.F0);
// 2nd row
map(keyCodes.BACK_QUOTE, BBC.ESCAPE);
map(keyCodes.K1, BBC.K1);
map(keyCodes.K2, BBC.K2);
map(keyCodes.K3, BBC.K3);
map(keyCodes.K4, BBC.K4);
map(keyCodes.K5, BBC.K5);
map(keyCodes.K6, BBC.K6);
map(keyCodes.K7, BBC.K7);
map(keyCodes.K8, BBC.K8);
map(keyCodes.K9, BBC.K9);
map(keyCodes.K0, BBC.K0);
map(keyCodes.MINUS, BBC.MINUS);
map(keyCodes.EQUALS, BBC.HAT_TILDE);
map(keyCodes.BACKSPACE, BBC.PIPE_BACKSLASH);
map(keyCodes.INSERT, BBC.LEFT);
map(keyCodes.HOME, BBC.RIGHT);
// 3rd row
map(keyCodes.LEFT_SQUARE_BRACKET, BBC.AT);
map(keyCodes.RIGHT_SQUARE_BRACKET, BBC.LEFT_SQUARE_BRACKET);
// no key for BBC.UNDERSCORE_POUND in UK
// see 4th row for US mapping keyCodes.BACKSLASH
map(keyCodes.DELETE, BBC.UP);
map(keyCodes.END, BBC.DOWN);
// 4th row
// no key for BBC.CAPSLOCK (mapped to CTRL_LEFT below)
map(keyCodes.CAPSLOCK, BBC.CTRL);
map(keyCodes.SEMICOLON, BBC.SEMICOLON_PLUS);
map(keyCodes.APOSTROPHE, BBC.COLON_STAR);
// UK prints `#~` on this key, a US board `\|`.
map(keyCodes.BACKSLASH, isUKlayout ? BBC.RIGHT_SQUARE_BRACKET : BBC.UNDERSCORE_POUND);
// Only a 102-key board has a key here, so only there can the left shift be spared.
map(keyCodes.SHIFT_LEFT, isUKlayout ? BBC.SHIFTLOCK : BBC.SHIFT);
map(keyCodes.INTL_BACKSLASH, BBC.SHIFT);
// 5th row
// for Zalaga
map(keyCodes.CTRL_LEFT, BBC.CAPSLOCK);
map(keyCodes.ALT_LEFT, BBC.CTRL);
// should be 4th row, not enough keys
map(keyCodes.MENU, BBC.DELETE);
map(keyCodes.CTRL_RIGHT, BBC.COPY);
// not in correct location
map(keyCodes.ALT_RIGHT, BBC.SHIFTLOCK);
} else {
// Physical, and default
map(keyCodes.K1, BBC.K1);
map(keyCodes.K2, BBC.K2);
map(keyCodes.K3, BBC.K3);
map(keyCodes.K4, BBC.K4);
map(keyCodes.K5, BBC.K5);
map(keyCodes.K6, BBC.K6);
map(keyCodes.K7, BBC.K7);
map(keyCodes.K8, BBC.K8);
map(keyCodes.K9, BBC.K9);
map(keyCodes.K0, BBC.K0);
map(keyCodes.SHIFT_LEFT, BBC.SHIFT);
map(keyCodes.EQUALS, BBC.HAT_TILDE); // ^~ on +/=
map(keyCodes.SEMICOLON, BBC.SEMICOLON_PLUS); // ';' / '+'
map(keyCodes.MINUS, BBC.MINUS); // '-' / '=' mapped to underscore
map(keyCodes.LEFT_SQUARE_BRACKET, BBC.LEFT_SQUARE_BRACKET); // maps to [{
map(keyCodes.RIGHT_SQUARE_BRACKET, BBC.RIGHT_SQUARE_BRACKET); // maps to ]}
map(keyCodes.COMMA, BBC.COMMA); // ',' / '<'
map(keyCodes.PERIOD, BBC.PERIOD); // '.' / '>'
map(keyCodes.SLASH, BBC.SLASH); // '/' / '?'
map(keyCodes.TAB, BBC.TAB); // tab
map(keyCodes.ENTER, BBC.RETURN); // return
map(keyCodes.DELETE, BBC.DELETE); // delete
map(keyCodes.BACKSPACE, BBC.DELETE); // delete
map(keyCodes.END, BBC.COPY); // copy key is end
map(keyCodes.HOME, BBC.SHIFTLOCK);
map(keyCodes.F11, BBC.COPY); // copy key is end for Apple
map(keyCodes.ESCAPE, BBC.ESCAPE); // escape
map(keyCodes.CTRL_LEFT, BBC.CTRL);
map(keyCodes.CTRL_RIGHT, BBC.CTRL);
map(keyCodes.CAPSLOCK, BBC.CAPSLOCK); // caps (on Rich's/Mike's computer)
map(keyCodes.LEFT, BBC.LEFT); // arrow left
map(keyCodes.UP, BBC.UP); // arrow up
map(keyCodes.RIGHT, BBC.RIGHT); // arrow right
map(keyCodes.DOWN, BBC.DOWN); // arrow down
map(keyCodes.APOSTROPHE, BBC.COLON_STAR);
// None of this last group in great locations.
// But better to have them mapped at least somewhere.
map(keyCodes.BACK_QUOTE, BBC.AT);
map(keyCodes.BACKSLASH, BBC.PIPE_BACKSLASH);
map(keyCodes.INTL_BACKSLASH, BBC.PIPE_BACKSLASH);
map(keyCodes.PAGEUP, BBC.UNDERSCORE_POUND);
}
// Master
map(keyCodes.NUMPAD0, BBC.NUMPAD0);
map(keyCodes.NUMPAD1, BBC.NUMPAD1);
map(keyCodes.NUMPAD2, BBC.NUMPAD2);
map(keyCodes.NUMPAD3, BBC.NUMPAD3);
map(keyCodes.NUMPAD4, BBC.NUMPAD4);
map(keyCodes.NUMPAD5, BBC.NUMPAD5);
map(keyCodes.NUMPAD6, BBC.NUMPAD6);
map(keyCodes.NUMPAD7, BBC.NUMPAD7);
map(keyCodes.NUMPAD8, BBC.NUMPAD8);
map(keyCodes.NUMPAD9, BBC.NUMPAD9);
map(keyCodes.NUMPAD_DECIMAL_POINT, BBC.NUMPAD_DECIMAL_POINT);
// "natural" mapping
map(keyCodes.NUMPADPLUS, BBC.NUMPADPLUS);
map(keyCodes.NUMPADMINUS, BBC.NUMPADMINUS);
map(keyCodes.NUMPADSLASH, BBC.NUMPADSLASH);
map(keyCodes.NUMPADASTERISK, BBC.NUMPADASTERISK);
//map(???, BBC.NUMPADCOMMA);
//map(???, BBC.NUMPADHASH);
map(keyCodes.NUMPADENTER, BBC.NUMPADENTER);
// TODO(#748) "game" mapping
// eg Master Dunjunz needs # Del 3 , * Enter
// https://web.archive.org/web/20080305042238/http://bbc.nvg.org/doc/games/Dunjunz-docs.txt
return finish();
}
/**
* Who holds SHIFT on a key matrix driven by mappings from a key map. A mapping of
* `[col, row, shiftOverride]` forces SHIFT to its override while that key is down; otherwise
* SHIFT follows the mapped shift key. Keys set on the matrix directly bypass it, and a SHIFT
* set that way survives plain mapped keys but not the shift key or a forcing key. Two mappings
* can share a matrix key (the Atom's arrows share its cursor keys), so a key stays down while
* any mapping holds it, and the latest forcing mapping still held decides SHIFT.
*/
export class ShiftOverride {
constructor([shiftCol, shiftRow]) {
this.shiftCol = shiftCol;
this.shiftRow = shiftRow;
this.held = new Map();
this.clear();
}
clear() {
this.physicalShiftDown = false;
this.forcedShift = null;
this.held.clear();
}
setMapped(keys, mapping, val) {
const [col, row, shiftOverride] = mapping;
const id = ShiftOverride.#heldId(mapping);
this.held.delete(id);
if (val) this.held.set(id, mapping);
const held = [...this.held.values()];
keys[col][row] = val || held.some(([c, r]) => c === col && r === row) ? 1 : 0;
const isShiftKey = shiftOverride === undefined && col === this.shiftCol && row === this.shiftRow;
if (isShiftKey) this.physicalShiftDown = !!val;
if (shiftOverride !== undefined) {
this.forcedShift = held.findLast(([, , override]) => override !== undefined)?.[2] ?? null;
}
if (isShiftKey || shiftOverride !== undefined || this.forcedShift !== null) {
keys[this.shiftCol][this.shiftRow] = (this.forcedShift ?? this.physicalShiftDown) ? 1 : 0;
}
}
// Part of keyboardState(), which shared-session peers on different deploys of one
// package version exchange.
state() {
return {
physicalShiftDown: this.physicalShiftDown,
shiftOverrideActive: this.forcedShift !== null,
shiftOverrideDesiredShift: !!this.forcedShift,
heldMappings: [...this.held.values()].map((mapping) => [...mapping]),
};
}
restore({ physicalShiftDown, shiftOverrideActive, shiftOverrideDesiredShift, heldMappings }) {
this.physicalShiftDown = physicalShiftDown;
this.forcedShift = shiftOverrideActive ? shiftOverrideDesiredShift : null;
this.held.clear();
for (const mapping of heldMappings ?? []) this.held.set(ShiftOverride.#heldId(mapping), mapping);
}
// By value, since a shared session's peers hand mappings over as copies.
static #heldId([col, row, shiftOverride]) {
return `${col},${row},${shiftOverride}`;
}
}
export const KeyMatrixSize = 16;
const KeyMatrixRows = 8;
/**
* The keyboard half of the BBC's system VIA and the Atom's PPIA: a key matrix that host keys are mapped onto. The
* owner calls `_initKeyMatrix` from its constructor, provides `keycodeToRowCol` (a key map) and `updateKeys()`, and
* sees every change through `_keysChanged()`.
* @template {new (...args: any[]) => object} TBase
* @param {TBase} Base
*/
export const WithKeyMatrix = (Base) =>
class extends Base {
/**
* @param {number[]} shiftKey where SHIFT is on the matrix
* @param {number} columns how many of the matrix's columns hold keys
*/
_initKeyMatrix(shiftKey, columns) {
this.keys = Array.from({ length: KeyMatrixSize }, () => new Uint8Array(KeyMatrixSize));
this.keyColumns = columns;
this.shift = new ShiftOverride(shiftKey);
this.keyboardEnabled = true;
/** @type {import("./replay.js").InputLog | null} */
this.inputLog = null;
}
/**
* The keys held down, which snapshots leave out so that a rewind or a loaded state
* keeps the keys the person is actually holding.
*/
keyboardState() {
return {
keys: this.keys.map((column) => Array.from(column)),
...this.shift.state(),
};
}
restoreKeyboard(state) {
state.keys.forEach((column, col) => this.keys[col].set(column));
this.shift.restore(state);
this.updateKeys();
}
clearKeys() {
for (const column of this.keys) column.fill(0);
this.shift.clear();
this._keysChanged();
}
disableKeyboard() {
this.keyboardEnabled = false;
this.clearKeys();
}
enableKeyboard() {
this.keyboardEnabled = true;
this.clearKeys();
}
set(key, val, shiftDown) {
const mapping = this.keyMapping(key, shiftDown);
Eif (mapping) this.setMapped(mapping, val);
}
/**
* Where a key code lands on the key matrix under the current layout, as `[col, row, shiftOverride?]`, or
* undefined.
*/
keyMapping(key, shiftDown) {
return this.keycodeToRowCol[!!shiftDown][key];
}
setMapped(mapping, val) {
if (!this.keyboardEnabled) return;
this.shift.setMapped(this.keys, mapping, val);
this._keysChanged();
}
keyDown(key, shiftDown) {
this.set(key, 1, shiftDown);
}
/** Releases the key under both the shifted and unshifted maps, whichever pressed it. */
keyUp(key) {
this.set(key, 0, true);
this.set(key, 0, false);
}
keyDownRaw([col, row]) {
this.keys[col][row] = 1;
this._keysChanged();
}
keyUpRaw([col, row]) {
this.keys[col][row] = 0;
this._keysChanged();
}
keyToggleRaw([col, row]) {
this.keys[col][row] = 1 - this.keys[col][row];
this._keysChanged();
}
_keysChanged() {
this.updateKeys();
this.inputLog?.record({ kind: "keyboard", state: this.keyboardState() });
}
hasAnyKeyDown() {
return this.keys
.slice(0, this.keyColumns)
.some((column) => column.subarray(0, KeyMatrixRows).some(Boolean));
}
};
|