| 1 |
<!doctype html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="UTF-8" /> |
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 |
<title>tinykeys</title> |
| 7 |
<script src="https://cdn.tailwindcss.com"></script> |
| 8 |
<style> |
| 9 |
.github-corner { |
| 10 |
fill: #151513; |
| 11 |
color: #fff; |
| 12 |
position: absolute; |
| 13 |
top: 0; |
| 14 |
border: 0; |
| 15 |
right: 0; |
| 16 |
} |
| 17 |
|
| 18 |
.github-corner:hover .octo-arm { |
| 19 |
animation: octocat-wave 560ms ease-in-out; |
| 20 |
} |
| 21 |
|
| 22 |
@keyframes octocat-wave { |
| 23 |
0%, |
| 24 |
100% { |
| 25 |
transform: rotate(0); |
| 26 |
} |
| 27 |
|
| 28 |
20%, |
| 29 |
60% { |
| 30 |
transform: rotate(-25deg); |
| 31 |
} |
| 32 |
|
| 33 |
40%, |
| 34 |
80% { |
| 35 |
transform: rotate(10deg); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
@media (max-width: 500px) { |
| 40 |
.github-corner:hover .octo-arm { |
| 41 |
animation: none; |
| 42 |
} |
| 43 |
|
| 44 |
.github-corner .octo-arm { |
| 45 |
animation: octocat-wave 560ms ease-in-out; |
| 46 |
} |
| 47 |
} |
| 48 |
</style> |
| 49 |
</head> |
| 50 |
<body class="pt-8 pb-12"> |
| 51 |
<div class="container mx-auto px-4"> |
| 52 |
<div class="flex flex-col gap-4"> |
| 53 |
<header> |
| 54 |
<h1 class="font-mono text-3xl font-bold text-indigo-600">tinykeys</h1> |
| 55 |
<p class="text-xl font-medium text-slate-600"> |
| 56 |
A tiny (~1KB) & modern library for keybindings. |
| 57 |
</p> |
| 58 |
</header> |
| 59 |
|
| 60 |
<a |
| 61 |
href="https://github.com/jamiebuilds/tinykeys" |
| 62 |
class="github-corner" |
| 63 |
aria-label="View source on GitHub" |
| 64 |
> |
| 65 |
<svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"> |
| 66 |
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path> |
| 67 |
<path |
| 68 |
d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" |
| 69 |
fill="currentColor" |
| 70 |
style="transform-origin: 130px 106px" |
| 71 |
class="octo-arm" |
| 72 |
></path> |
| 73 |
<path |
| 74 |
d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" |
| 75 |
fill="currentColor" |
| 76 |
></path> |
| 77 |
</svg> |
| 78 |
</a> |
| 79 |
|
| 80 |
<pre |
| 81 |
class="my-4 rounded-md bg-slate-900 p-3 font-mono text-lg text-white before:mr-2 before:text-indigo-400 before:content-['$']" |
| 82 |
> |
| 83 |
npm install tinykeys</pre |
| 84 |
> |
| 85 |
|
| 86 |
<h2 class="text-2xl font-bold">Examples</h2> |
| 87 |
<p>Try some of the following:</p> |
| 88 |
<ul class="flex list-inside list-disc flex-col gap-4 pl-3"> |
| 89 |
<li> |
| 90 |
<kbd |
| 91 |
class="rounded bg-emerald-100 px-1 py-px font-mono text-emerald-700" |
| 92 |
>Shift+D</kbd |
| 93 |
> |
| 94 |
</li> |
| 95 |
<li> |
| 96 |
<kbd |
| 97 |
class="rounded bg-emerald-100 px-1 py-px font-mono text-emerald-700" |
| 98 |
>y e e t</kbd |
| 99 |
> |
| 100 |
</li> |
| 101 |
<li> |
| 102 |
<kbd |
| 103 |
class="rounded bg-emerald-100 px-1 py-px font-mono text-emerald-700" |
| 104 |
>Control+1..9</kbd |
| 105 |
> |
| 106 |
(Windows/Linux) or |
| 107 |
<kbd |
| 108 |
class="rounded bg-emerald-100 px-1 py-px font-mono text-emerald-700" |
| 109 |
>Command+1..9</kbd |
| 110 |
> |
| 111 |
(Mac) |
| 112 |
</li> |
| 113 |
</ul> |
| 114 |
|
| 115 |
<pre class="rounded-md bg-slate-900 p-3 font-mono text-white"> |
| 116 |
import tinykeys from "tinykeys" |
| 117 |
|
| 118 |
tinykeys(window, { |
| 119 |
<strong class="text-emerald-400">"Shift+D"</strong>: () => { |
| 120 |
<em class="text-pink-400">alert("The 'Shift' and 'd' keys were pressed at the same time")</em> |
| 121 |
}, |
| 122 |
<strong class="text-emerald-400">"y e e t"</strong>: () => { |
| 123 |
<em class="text-pink-400">alert("The keys 'y', 'e', 'e', and 't' were pressed in order")</em> |
| 124 |
}, |
| 125 |
<strong class="text-emerald-400">"$mod+([1-9])"</strong>: event => { |
| 126 |
event.preventDefault() |
| 127 |
<em class="text-pink-400">alert(`Either 'Control+${event.key}' or 'Meta+${event.key}' were pressed`)</em> |
| 128 |
}, |
| 129 |
}) |
| 130 |
<details><summary class="text-slate-500 bg-slate-800 rounded p-1 my-1">what does this button do</summary>import confetti from "canvas-confetti" |
| 131 |
|
| 132 |
<strong class="text-emerald-400">let KonamiCode = [ |
| 133 |
"ArrowUp", |
| 134 |
"ArrowUp", |
| 135 |
"ArrowDown", |
| 136 |
"ArrowDown", |
| 137 |
"ArrowLeft", |
| 138 |
"ArrowRight", |
| 139 |
"ArrowLeft", |
| 140 |
"ArrowRight", |
| 141 |
"KeyB", |
| 142 |
"KeyA", |
| 143 |
"Enter", |
| 144 |
].join(" ")</strong> |
| 145 |
|
| 146 |
tinykeys(window, { |
| 147 |
[<strong class="text-emerald-400">KonamiCode</strong>]: () => { |
| 148 |
<em class="text-pink-400">confetti()</em> |
| 149 |
}, |
| 150 |
})</details></pre> |
| 151 |
|
| 152 |
<h2 class="text-2xl font-bold" id="debugger">Debugger</h2> |
| 153 |
<p>Typing will log details of the event that was fired</p> |
| 154 |
<div |
| 155 |
class="relative h-[theme(spacing.96)] w-full overflow-auto rounded bg-slate-300" |
| 156 |
> |
| 157 |
<table class="w-full text-center"> |
| 158 |
<thead> |
| 159 |
<tr class="sticky top-0"> |
| 160 |
<th class="bg-slate-100 p-2 odd:bg-slate-200"> |
| 161 |
<code class="font-mono text-xs">event.key</code> |
| 162 |
</th> |
| 163 |
<th class="bg-slate-100 p-2 odd:bg-slate-200"> |
| 164 |
<code class="font-mono text-xs">event.code</code> |
| 165 |
</th> |
| 166 |
<th class="bg-slate-100 p-2 odd:bg-slate-200"> |
| 167 |
<code class="font-mono text-xs" |
| 168 |
>event.getModifierState(event.key)</code |
| 169 |
> |
| 170 |
</th> |
| 171 |
<th class="bg-slate-100 p-2 odd:bg-slate-200"> |
| 172 |
<code class="font-mono text-xs" |
| 173 |
>event.getModifierState(...)</code |
| 174 |
> |
| 175 |
</th> |
| 176 |
<th class="bg-slate-100 p-2 odd:bg-slate-200"> |
| 177 |
<code class="font-mono text-xs">event.repeat</code> |
| 178 |
</th> |
| 179 |
<th class="bg-slate-100 p-2 odd:bg-slate-200"> |
| 180 |
<code class="font-mono text-xs">event.isComposing</code> |
| 181 |
</th> |
| 182 |
<th class="bg-slate-100 p-2 odd:bg-slate-200"> |
| 183 |
<code class="font-mono text-xs">event.location</code> |
| 184 |
</th> |
| 185 |
</tr> |
| 186 |
</thead> |
| 187 |
<tbody id="logs"></tbody> |
| 188 |
</table> |
| 189 |
</div> |
| 190 |
<blockquote |
| 191 |
class="rounded border-l-4 bg-slate-100 p-3 text-sm text-slate-700" |
| 192 |
> |
| 193 |
<p> |
| 194 |
<strong>Note:</strong> |
| 195 |
<code>event.getModifierState(event.key)</code> tests if the key |
| 196 |
pressed is considered a "modifier" key |
| 197 |
</p> |
| 198 |
</blockquote> |
| 199 |
</div> |
| 200 |
</div> |
| 201 |
<script type="module" src="./index.tsx"></script> |
| 202 |
<script> |
| 203 |
let logsElement = document.querySelector("#logs") |
| 204 |
window.addEventListener("keydown", event => { |
| 205 |
let wellKnownModifiers = [ |
| 206 |
"Alt", |
| 207 |
"AltGraph", |
| 208 |
"CapsLock", |
| 209 |
"Control", |
| 210 |
"Fn", |
| 211 |
"FnLock", |
| 212 |
"Hyper", |
| 213 |
"Meta", |
| 214 |
"NumLock", |
| 215 |
"OS", |
| 216 |
"ScrollLock", |
| 217 |
"Shift", |
| 218 |
"Super", |
| 219 |
"Symbol", |
| 220 |
"SymbolLock", |
| 221 |
// legacy |
| 222 |
"Accel", |
| 223 |
"Scroll", |
| 224 |
"Win", |
| 225 |
] |
| 226 |
let newLog = { |
| 227 |
key: event.key, |
| 228 |
code: event.code, |
| 229 |
isModifier: event.getModifierState(event.key), |
| 230 |
activeModifiers: new Set(), |
| 231 |
repeat: event.repeat, |
| 232 |
isComposing: event.isComposing, |
| 233 |
location: event.location, |
| 234 |
} |
| 235 |
|
| 236 |
for (let modifier of wellKnownModifiers) { |
| 237 |
if (event.getModifierState(modifier)) { |
| 238 |
newLog.activeModifiers.add(modifier) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
let tr = document.createElement("tr") |
| 243 |
tr.innerHTML = ` |
| 244 |
<td class="bg-slate-50 odd:bg-slate-100 p-2 text-indigo-700 font-bold"><code>${ |
| 245 |
newLog.key |
| 246 |
}</code></td> |
| 247 |
<td class="bg-slate-50 odd:bg-slate-100 p-2 text-indigo-700 font-bold"><code>${ |
| 248 |
newLog.code |
| 249 |
}</code></td> |
| 250 |
<td class="bg-slate-50 odd:bg-slate-100 p-2"><code>${ |
| 251 |
newLog.isModifier |
| 252 |
}</code></td> |
| 253 |
<td class="bg-slate-50 odd:bg-slate-100 p-2"><code>${ |
| 254 |
Array.from(newLog.activeModifiers).join(", ") || "(none)" |
| 255 |
}</code></td> |
| 256 |
<td class="bg-slate-50 odd:bg-slate-100 p-2"><code>${newLog.repeat}</code></td> |
| 257 |
<td class="bg-slate-50 odd:bg-slate-100 p-2"><code>${ |
| 258 |
newLog.isComposing |
| 259 |
}</code></td> |
| 260 |
<td class="bg-slate-50 odd:bg-slate-100 p-2"><code>${ |
| 261 |
newLog.location |
| 262 |
}</code></td> |
| 263 |
` |
| 264 |
|
| 265 |
logsElement.prepend(tr) |
| 266 |
}) |
| 267 |
</script> |
| 268 |
</body> |
| 269 |
</html> |