Website publishing
The Page Scrolls Instead of the Game: Keyboard Focus on Embed Pages
Keys go to whichever document has focus. A click-to-play cover, honest instructions, touch-scroll gutters and accessibility rules keep arrow keys and Space working.
A visitor opens your game page on a laptop, presses the spacebar to start, and the page scrolls down instead. They press the arrow keys and the whole page moves. They leave. It is one of the most common frustrations on pages with embedded games, and it has a simple cause: keyboard input goes to whichever element has focus, and until the player clicks inside the game, the focus is on your page, not on the iframe. This post explains the mechanism and the fixes, in order from least to most invasive.
Why it happens
An iframe is a separate document. Key presses go to the document that has focus. When your page loads, the focus is on the page, so arrow keys, Space, Page Down and the like do what they always do on a page: scroll it. Only after the player clicks inside the game does the iframe take focus, and from then on it receives the keys. A game cannot fix this from the inside, because it does not receive the key events until it has focus.
The same issue explains why games sometimes seem to ignore the first keys after the page loads.
Fix one: tell the player to click first
The cheapest fix is a line of text near the game: "Click the game once to enable keyboard controls". It is honest and costs nothing. Make it visible, not tiny grey text in a footer.
Fix two: a click-to-play cover
A better approach is to place a cover over the game with a Play button. Clicking it removes the cover and focuses the iframe, so the next key press goes to the game. It has other benefits too: the game loads only on click (faster page), and audio is allowed to start because a user gesture happened.
<div class="game-wrap">
<button class="play-cover" type="button">Play</button>
<iframe id="game" title="Game name" data-src="https://playkovo.com/embed/GAME-SLUG"
allow="fullscreen; autoplay" tabindex="0"></iframe>
</div>
<script>
const cover = document.querySelector('.play-cover');
const frame = document.getElementById('game');
cover.addEventListener('click', () => {
frame.src = frame.dataset.src;
cover.remove();
frame.addEventListener('load', () => frame.focus(), { once: true });
frame.focus();
});
</script>
Check how this interacts with page speed. Loading the iframe on demand is often a speed win, as discussed in HTML5 game embeds and Core Web Vitals and lazy loading many embeds. Do keep the cover accessible: it is a real button, reachable with Tab and activated with Enter.
Fix three: stop the page scrolling only when the game is active
Some sites try to block keys on the whole page. Do not. Preventing the default action of the arrow keys and Space across the page breaks scrolling for everyone, including people who use the keyboard to navigate and people with assistive technology. If you must act, be narrow:
let gameActive = false;
frame.addEventListener('focus', () => gameActive = true);
frame.addEventListener('blur', () => gameActive = false);
window.addEventListener('keydown', e => {
const keys = ['ArrowUp','ArrowDown','ArrowLeft','ArrowRight',' '];
if (gameActive && keys.includes(e.key)) e.preventDefault();
});
Even here, key events inside the iframe never reach your parent window, so this only helps in the moments focus bounces. The cover approach is better. Consider whether you need this fix at all.
Touch scrolling and the iframe trap
On phones the problem looks different. A game that uses swipes or drags captures touches inside the iframe, so a visitor who swipes up to scroll your page lands in the game, and the page does not move. Give the visitor a way past:
- Keep a margin of page content around the game, so there is space to start a scroll outside the frame.
- For tall pages, make sure the game does not fill the whole screen height, or add a scroll handle above and below it.
- Offer an expanded or fullscreen mode, so the game gets the whole viewport when it needs it. See fullscreen on iPhone for embedded games.
Do not break accessibility
- Give the iframe a descriptive
titleattribute. Screen reader users depend on it. - Do not trap keyboard focus inside the game. People using the keyboard must be able to Tab out. Many games capture Tab, so provide a visible "skip past the game" link after the iframe.
- Make the cover and the skip link focusable and visible on focus.
The wider checklist is in accessible game pages: title, focus and keyboard.
Test it in thirty seconds
- Load the page fresh. Do not click anywhere. Press Space and the arrow keys. What happens?
- Click Play. Press the same keys. Does the game respond and the page stay still?
- Press Tab repeatedly. Can you reach the next link after the game?
- Repeat on a phone: swipe over the game, then swipe outside it.
If any of those fails, you have found something visitors will hit. The most useful single change on most sites is the click-to-play cover.


