🎮ArcadeLab

Why a held pause key can restart your game

by BoldWave79
106 lines5.5 KB
▶ Play
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Why a held pause key can restart your game</title>
<style>
* {box-sizing:border-box}
body {margin:0;background:#172431;color:#f1f5f7;font:17px/1.6 system-ui,sans-serif}
main {max-width:750px;margin:auto;padding:26px clamp(16px,4vw,40px) 52px}
h1,h2,h3 {line-height:1.15;text-wrap:balance}
h1 {font-size:clamp(32px,6vw,52px);margin:0 0 22px}
h2 {font-size:26px;margin:32px 0 14px}
p {margin:12px 0}
section.lab {background:#0c1721;border:1px solid #628497;border-radius:12px;padding:8px 16px 18px}
.pair {display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:10px;margin:12px 0}
.card {padding:12px;background:#243b48;border-radius:7px}
.card h3 {font-size:16px;margin:0 0 3px}
button {margin:6px 5px 6px 0;padding:8px 12px;border:1px solid #b7d8e7;border-radius:6px;background:#193344;color:#fff;font:inherit;cursor:pointer}
:focus-visible {outline:3px solid #ffd767;outline-offset:3px}
output {font-weight:650}
footer {border-top:1px solid #66818b;margin-top:26px;padding-top:8px;font-size:15px}
a {color:#a4e4ff}
</style>
</head>
<body>
<main>
<article>
<h1>Why a held pause key can restart your game</h1>
<p>Imagine stopping a browser game just before a difficult jump. You press P, take a moment to lift your finger, and the game is already running again. One physical press can deliver several keyboard events. In this experiment, feed the same press to two copies of a pause menu and see exactly where they disagree.</p>
<h2>Try a tap, then a hold</h2>
<p>Both copies begin in play. Select the keyboard pad, tap the letter P once and release it. Both copies should pause. Reset them, select the pad again and hold P until the first counter moves more than once. Then release. A keyboard with repeat switched off may give you just one event. The replay button works on a touch screen and always supplies the same sequence.</p>
<section class="lab" aria-label="Interactive pause comparison">
<button id="pad" type="button">Keyboard pad</button>
<button id="replay" type="button">Replay one press with three repeats</button>
<button id="reset" type="button">Reset both menus</button>
<div class="pair">
<div class="card"><h3>Every keydown</h3><output id="any">Playing · 0 switches</output></div>
<div class="card"><h3>First keydown only</h3><output id="first">Playing · 0 switches</output></div>
</div>
<p>Input events in order</p>
<output id="log">Waiting for input.</output>
</section>
<h2>Read the two counters</h2>
<p>The replay starts a fresh trial. It supplies one initial keydown, three repeating keydowns and one keyup. The first menu changes state four times and ends up playing. The second changes once and stays paused. The release appears in the event list and changes neither menu. A shorter hold on your own keyboard can leave both menus paused even when the first one switched three times. Looking at the final screen alone hides that error.</p>
<h2>Give each action a rule</h2>
<p>For a pause command, inspect the repeat flag on the keydown event. Switch the menu only when the event belongs to the chosen key and repeat is false. For walking, a different rule works better. Remember that the movement key is down, let the game loop advance the character while it stays down, and clear the flag on release. Otherwise the pace of movement can depend on the repeat delay set on the player's computer.</p>
<p>To check a fix, try two separate quick taps after a reset. Both menus should switch twice and finish in play. Next, run the held-key replay. A pause command that passes both trials accepts a genuine second press while ignoring the extra events from the first one.</p>
<footer>
<p>Technical reference: <a href="https://www.w3.org/TR/uievents/#dom-keyboardevent-repeat" target="_blank" rel="noopener noreferrer">W3C UI Events, KeyboardEvent.repeat</a>.</p>
<p>By the ARTCORE editorial team. AI assisted with drafting and building this interactive example.</p>
</footer>
</article>
</main>
<script>
const pad = document.querySelector('#pad');
const any = document.querySelector('#any');
const first = document.querySelector('#first');
const log = document.querySelector('#log');
let everySwitch = 0;
let firstSwitch = 0;
let history = [];
function counter(count) {
  const state = count % 2 ? 'Paused' : 'Playing';
  return state + ' · ' + count + (count === 1 ? ' switch' : ' switches');
}
function render() {
  any.value = counter(everySwitch);
  first.value = counter(firstSwitch);
  log.value = history.length ? history.join(' → ') : 'Waiting for input.';
}
function reset() {
  everySwitch = 0;
  firstSwitch = 0;
  history = [];
  render();
}
function record(kind, repeating = false) {
  if (kind === 'down') {
    everySwitch += 1;
    if (!repeating) firstSwitch += 1;
  }
  history.push(kind === 'up' ? 'up' : (repeating ? 'repeat' : 'down'));
  if (history.length > 12) history.shift();
  render();
}
function isPauseKey(event) {
  return event.key.toLowerCase() === 'p' && !event.ctrlKey && !event.metaKey && !event.altKey;
}
pad.addEventListener('keydown', event => {
  if (isPauseKey(event)) record('down', event.repeat);
});
pad.addEventListener('keyup', event => {
  if (isPauseKey(event)) record('up');
});
document.querySelector('#reset').addEventListener('click', reset);
document.querySelector('#replay').addEventListener('click', () => {
  reset();
  record('down');
  for (let index = 0; index < 3; index++) record('down', true);
  record('up');
});
</script>
</body>
</html>

Game Source: Why a held pause key can restart your game

Creator: BoldWave79

Libraries: none

Complexity: moderate (106 lines, 5.5 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: why-a-held-pause-key-can-restart-your-ga-boldwave79" to link back to the original. Then publish at arcadelab.ai/publish.