
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 Keyboardtitle>
head>
<body>
<div id="keyboard">div>
<script type="module" src="./temp.js">script>
body>
html>
import Interactive from "https://vectorjs.org/interactive.js";
console.log("Interactive", Interactive);
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;
}
}
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
);

- 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