使用html画一个键盘

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://vectorjs.org/library.css">
  <title>vector examples Keyboard</title>
</head>
<body>
  <div id="keyboard"></div>
  <!-- <script type="module" src="./Keyboard.js"></script> -->
  <script type="module" src="./temp.js"></script>
</body>
</html>
js 复制代码
import Interactive from "https://vectorjs.org/interactive.js";
console.log("Interactive", Interactive);
js 复制代码
/**
 * @title Keyboard
 * @description This interactive demonstrates how key board input can be used to add interactivity.
 * @tags [input]
 */
// import { Interactive, getScriptName } from "../../index.js";
// let interactive = new Interactive(getScriptName());
import Interactive from "https://vectorjs.org/interactive.js";
let interactive = new Interactive("keyboard");
interactive.width = 768;
interactive.height = 300;
interactive.border = true;
let buffer = "";
let keys = [
  [
    "`",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "0",
    "-",
    "=",
    "Backspace",
  ],
  ["tab", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\"],
  ["CapsLock", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "Enter"],
  ["Shift", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "Shift"],
  [
    "fn",
    "Control",
    "Alt",
    "Meta",
    " ",
    "Meta",
    "Alt",
    "ArrowLeft",
    "ArrowUp",
    "ArrowDown",
    "ArrowRight",
  ],
];
let buttons = [];
let keycodes = {
  0: { Symbol: 0, Shift: ")" },
  1: { Symbol: 1, Shift: "!" },
  2: { Symbol: 2, Shift: "@" },
  3: { Symbol: 3, Shift: "#" },
  4: { Symbol: 4, Shift: "$" },
  5: { Symbol: 5, Shift: "%" },
  6: { Symbol: 6, Shift: "^" },
  7: { Symbol: 7, Shift: "&" },
  8: { Symbol: 8, Shift: "*" },
  9: { Symbol: 9, Shift: "(" },
  a: { Symbol: "a", Shift: "A" },
  b: { Symbol: "b", Shift: "B" },
  c: { Symbol: "c", Shift: "C" },
  d: { Symbol: "d", Shift: "D" },
  e: { Symbol: "e", Shift: "E" },
  f: { Symbol: "f", Shift: "F" },
  g: { Symbol: "g", Shift: "G" },
  h: { Symbol: "h", Shift: "H" },
  i: { Symbol: "i", Shift: "I" },
  j: { Symbol: "j", Shift: "J" },
  k: { Symbol: "k", Shift: "K" },
  l: { Symbol: "l", Shift: "L" },
  m: { Symbol: "m", Shift: "M" },
  n: { Symbol: "n", Shift: "N" },
  o: { Symbol: "o", Shift: "O" },
  p: { Symbol: "p", Shift: "P" },
  q: { Symbol: "q", Shift: "Q" },
  r: { Symbol: "r", Shift: "R" },
  s: { Symbol: "s", Shift: "S" },
  t: { Symbol: "t", Shift: "T" },
  u: { Symbol: "u", Shift: "U" },
  v: { Symbol: "v", Shift: "V" },
  w: { Symbol: "w", Shift: "W" },
  x: { Symbol: "x", Shift: "X" },
  y: { Symbol: "y", Shift: "Y" },
  z: { Symbol: "z", Shift: "Z" },
  "`": { Symbol: "`", Shift: "~" },
  "-": { Symbol: "-", Shift: "_" },
  "=": { Symbol: "=", Shift: "+" },
  ";": { Symbol: ";", Shift: ":" },
  "'": { Symbol: "'", Shift: '"' },
  "[": { Symbol: "[", Shift: "{" },
  "]": { Symbol: "]", Shift: "}" },
  "\\": { Symbol: "\\", Shift: "|" },
  ",": { Symbol: ",", Shift: "<" },
  ".": { Symbol: ".", Shift: ">" },
  "/": { Symbol: "/", Shift: "?" },
  Backspace: { Symbol: "⌫", Shift: "" },
  tab: { Symbol: "  ", Shift: "" },
  CapsLock: { Symbol: "⇪", Shift: "" },
  Shift: { Symbol: "⇧", Shift: "" },
  Enter: { Symbol: "⏎", Shift: "" },
  Control: { Symbol: "⌃", Shift: "" },
  Alt: { Symbol: "Alt", Shift: "" },
  Meta: { Symbol: "⌘", Shift: "" },
  ArrowLeft: { Symbol: "←", Shift: "" },
  ArrowUp: { Symbol: "↑", Shift: "" },
  ArrowDown: { Symbol: "↓", Shift: "" },
  ArrowRight: { Symbol: "→", Shift: "" },
  " ": { Symbol: " ", Shift: "" },
};
let buttonMap = new Map();
let height = 32;
let margin = 8;
let capslock = interactive.button(0, 0, keycodes["CapsLock"].Symbol);
let shift = interactive.button(0, 0, keycodes["Shift"].Symbol);
for (let row = 0; row < keys.length; row++) {
  let x = 32;
  let prev;
  for (let i = 0; i < keys[row].length; i++) {
    let key = keys[row][i];
    let width = 32;
    let button;
    switch (key) {
      case "CapsLock":
        button = capslock;
        width = 64;
        break;
      case "Shift":
        if (shift.x === 0) {
          button = shift;
        } else {
          button = interactive.button(
            0,
            0,
            keycodes[key] != undefined ? keycodes[key].Symbol : key
          );
        }
        width = 90;
        break;
      case "tab":
        width = 50;
        button = interactive.button(
          0,
          0,
          keycodes[key] != undefined ? keycodes[key].Symbol : key
        );
        break;
      case " ":
        width = 176;
        button = interactive.button(
          0,
          0,
          keycodes[key] != undefined ? keycodes[key].Symbol : key
        );
        break;
      case "Meta":
        width = 50;
        button = interactive.button(
          0,
          0,
          keycodes[key] != undefined ? keycodes[key].Symbol : key
        );
        break;
      default:
        button = interactive.button(
          0,
          0,
          keycodes[key] != undefined ? keycodes[key].Symbol : key
        );
        if (keycodes[key] != undefined) {
          if (keycodes[key].Shift != "") {
            button.addDependency(shift, capslock);
            button.update = () => {
              if (shift.active || capslock.active) {
                button.label.contents = keycodes[key].Shift;
              } else {
                button.label.contents = keycodes[key].Symbol;
              }
            };
          }
        }
    }
    button.x = x;
    button.y = 64 + row * (height + margin);
    if (button.box.width < width) {
      button.box.width = width;
    }
    let bbox = button.root.getBBox();
    x += bbox.width + margin;
    buttons.push(button);
    prev = button;
    buttonMap.set(key, button);
    if (key === "ArrowUp") {
      button.box.height = 16;
      button.label.root.style.display = "none";
    } else if (key === "ArrowDown") {
      button.box.height = 16;
      button.x -= 32 + margin;
      button.y += 16;
      button.label.root.style.display = "none";
      x -= bbox.width + margin;
    }
  }
  let right = prev.x;
  if (right < 630) {
    prev.box.width = 630 - right;
  }
}
// console.log(keycodes["0"], shift, capslock);
let active = [];
window.onkeydown = function (event) {
  for (let i = 0; i < buttons.length; i++) {
    let button = buttons[i];
    if (
      button.label.contents === event.key ||
      (keycodes[event.key] != undefined &&
        button.label.contents === keycodes[event.key].Symbol)
    ) {
      button.box.style.fill = "#f8f8f8";
      button.label.style.fill = "#404040";
      button.active = true;
      active.push(button);
    }
  }
  buffer += `'${event.key}',`;
};
window.onkeyup = function (event) {
  let newActive = [];
  for (let button of active) {
    if (
      button.label.contents === event.key ||
      (keycodes[event.key] != undefined &&
        button.label.contents === keycodes[event.key].Symbol)
    ) {
      button.box.style.fill = "";
      button.label.style.fill = "";
      button.active = false;
    } else {
      newActive.push(button);
    }
  }
  active = newActive;
};
let bbox = interactive.input.root.getBBox();
interactive.setViewBox(
  bbox.x - margin,
  bbox.y,
  bbox.width + 2 * margin,
  bbox.height
);
//# sourceMappingURL=keyboard.js.map
相关推荐
崔庆才丨静觅6 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60617 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了7 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅7 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅8 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅8 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment8 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅8 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊8 小时前
jwt介绍
前端
爱敲代码的小鱼9 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax