Metlivi Blog

How to Keep Keyboard Controls from Breaking a Mouse-Driven Puzzle Slide

If you are building a browser-based sliding puzzle as a tabletop adventure aid, make the mouse drag and keyboard moves operate on the same puzzle state, while keeping keyboard shortcuts scoped to the puzzle. The key checks are focus, event handling, and drag completion: a key press in a text field should not move a tile, and a drag should not be interrupted or lost when the pointer crosses a tile boundary.

October 15, 20266 min readBy Metlivi Editorial Team
Section 1

Start by identifying what is moving the puzzle unexpectedly

When a puzzle reacts to an arrow key during a mouse operation, the cause is often an overly broad keyboard listener. A handler attached to document or window can receive arrow keys even when the player is typing in a clue field, using a menu, or interacting with another control. It may also trigger browser behavior, such as scrolling, at the same time that it changes the puzzle.

Reproduce the problem with a short checklist: click a tile and drag it; drag across the puzzle boundary; press an arrow key while the puzzle has focus; then press one while a clue input, button, or menu has focus. Note whether the board changes, the page scrolls, focus moves, or the drag stops. These observations distinguish a puzzle handler problem from pointer tracking or browser default behavior.

Section 2

Give the puzzle an explicit focus boundary

Treat the board as a control with a clear keyboard entry point. Use a focusable board container and handle puzzle keys only when that board is active, or when focus is on a tile that the board owns. Keep unrelated controls, such as hint buttons and text fields, outside that key-handling boundary. Make the focus location visible so a player can tell when the board will respond.

Check the event target before interpreting a key. If focus is in an input, text area, editable element, or a separate button, leave the key alone. For a cooperative adventure tool, this matters when a player is entering a rune, name, or clue while another person manipulates the puzzle: typing should not become accidental tile movement.

Keyboard operation still needs to work. W3C’s guidance explains that functionality should be available through a keyboard interface where possible, and identifies moving objects to a location as an action that can be keyboard operated. Scope the keys carefully instead of removing them altogether. W3C: Understanding Success Criterion 2.1.1: Keyboard

Section 3

Separate puzzle commands from browser defaults

Choose a small, documented set of keys for the board, such as arrow keys when the board is focused. In the handler, first confirm that the key is one the puzzle uses and that the board is the active target. Only then apply the move and, if necessary, cancel the browser’s default action for that key. Avoid suppressing every key globally: players may need normal typing, browser navigation, and controls outside the puzzle.

preventDefault() cancels a browser default action when the event is cancelable, but it does not stop the event from propagating to other handlers. That means it can prevent page scrolling without fixing a second listener that also moves a tile. Find and narrow the handler that changes game state; do not use broad cancellation as a substitute for clear event ownership. MDN: Event: preventDefault() method

Section 4

Keep pointer dragging intact across the board

For a drag interaction, track a single active pointer from the press that begins the drag through its release or cancellation. Pointer events provide pointerdown, pointermove, pointerup, and pointercancel; pointer capture can keep later pointer events directed to the board even when the pointer moves outside its original hit area. Clear the active drag on both pointerup and pointercancel, so an interrupted gesture does not leave the puzzle in a stuck dragging state. MDN: Pointer events

Avoid rebuilding or replacing the dragged tile in a way that loses its pointer tracking. If the interface moves an element in the DOM during a drag, MDN notes that pointer capture should be set after that move. Decide what happens when the pointer is released over an invalid position: for a sliding puzzle, restoring the tile to its prior position or snapping it to a legal position is easier to understand than silently changing unrelated tiles.

Section 5

Make the same move available without dragging

A puzzle can support mouse, keyboard, and simple pointer input together. For example, clicking or tapping a tile adjacent to the empty space could move it; dragging that tile could remain an optional direct-manipulation gesture; and focused-board arrow keys could provide keyboard operation. These inputs should all call the same move-validation logic, so a move is legal or rejected consistently regardless of how it was requested.

This also gives a useful design check: can a player complete the puzzle with a click or tap and no drag? W3C’s WCAG 2.2 guidance treats keyboard access and a single-pointer alternative to dragging as separate concerns. Its examples include replacing a drag with clicks on directional controls. A keyboard-only alternative does not itself provide that pointer alternative. W3C: Understanding Success Criterion 2.5.7: Dragging Movements

For a sliding puzzle, clicking one neighboring tile at a time is often a natural alternative because it preserves the puzzle’s adjacent-move rule. If that interaction would be unclear, visible move buttons can explain the available direction. W3C’s G219 technique gives examples of single-pointer controls that perform the same underlying action without dragging. W3C: G219: Ensuring that an alternative is available for dragging movements that operate on content

Section 6

Test with a short input matrix

Before using the puzzle at the table, run through each input path with the same board layout and starting state. Confirm that a valid move updates the board once, an invalid move leaves it unchanged, and unrelated controls keep their normal behavior.

  • Mouse: drag a legal tile, release outside the board, and attempt an illegal move. Confirm the board neither loses the drag nor changes unexpectedly.

  • Keyboard: focus the board, use each supported direction, then move focus to a text field and type. Confirm only board-focused keys move tiles and that typing does not scroll or alter the board.

  • Pointer without dragging: click or tap a legal neighboring tile. Confirm it produces the same move as the other input methods.

  • Interruption: cancel or abandon a drag, then start another. Confirm no stale selection remains.

If a test fails, inspect the narrowest relevant layer first: the event target and focus state for keyboard behavior; pointer capture and cleanup for interrupted drags; and the shared move validator for inconsistent rules. This sequence gives the builder a concrete way to locate the problem without turning the adventure prop into a general-purpose control tutorial.

Section 7

Sources

Related reading

Keep exploring this topic