KEYBOARD INPUTS FOR
FIRST PERSON GAMES
By Shane Brumback · · Updated · Free Three.js tutorial · Level 1
To program keyboard input for a Three.js first person game, keep a keys object that keydown sets to true and keyup sets to false, then read it every frame to build a movement vector, rotate it by the player yaw and move the player by speed times delta time. This demo adds Shift to sprint, Space to jump, V to swap first and third person, and touch buttons for phones.
A beginner walkthrough of keydown and keyup, WASD and arrow key movement, gravity, simple collisions and a first person camera for a Three.js 3D browser game. The neon arena behind this page is the live demo running on autopilot.
LIVE DEMO
The neon arena behind this page is a live Three.js game scene on autopilot. Launch the full-screen demo to walk with WASD or the arrow keys, sprint with Shift, jump with Space, press V to swap camera views and drag to look around. On a phone, use the on-screen keys. Launch asks for a free Google sign-in first. Press Esc or Exit to return.
Free Google sign-in · WASD / arrows · Shift sprint · Space jump · V view · Drag to look · Esc to exit
Why Games Track Keys Instead of Reacting to Them
A keyboard event fires once when a key goes down and once when it comes up. Holding W does not send a steady stream you can rely on (key repeat is slow and depends on the computer). So games do not move the player inside the event. They just write down which keys are held, in a simple keys object, and the render loop reads that list 60 times a second.
That one idea fixes the classic beginner bugs: movement that stutters, two keys that will not work together, and a player who keeps walking after you switch tabs. We use e.code (like KeyW) instead of e.key, because code is the physical key position, so WASD still works on French AZERTY and other layouts.
From Keys to Movement
Each frame we turn the held keys into two numbers: forward (W minus S) and strafe (D minus A). Those make a direction arrow. We normalize it so walking diagonally is not faster, then rotate it by the way the player is facing (the yaw) so W always means “the way I am looking”. Multiply by speed and by the frame time, and the player moves the same distance per second on a 60 Hz laptop or a 144 Hz monitor.
Jumping is a vertical speed that gravity pulls down every frame. Collisions here are simple circles around each crate that push the player back out. For mouse look with a locked cursor, see Three.js Pointer Lock Controls, and for a full shooter start from the FPS game starter.
How It Works
- Set up the scene, camera, renderer and lights. We make a dark blue world with fog, a wide first person camera, a renderer, and soft sky light plus a sun.
- Build a neon arena with colorful crates. We lay a glowing grid floor and scatter bright crates to walk around, remembering how big each one is for collisions.
- Make the player and its state. The player is a white capsule with a glowing visor, and a small state object remembers where it looks, how fast it falls and which camera view is on.
- Track held keys with keydown and keyup. Pressing a key writes true next to its name, letting go writes false, and switching away from the tab lets go of everything.
- Draw an on-screen key HUD that also works for touch. We draw W, A, S, D, Shift, Jump and V buttons that light up when held, and on a phone you can press them with your thumb.
- Drag to look around. Dragging the mouse or a finger across the game turns your head left, right, up and down.
- Add an autopilot that presses keys for you. Until you touch anything, a tiny robot holds W, taps A and D, sprints now and then and jumps every few seconds, so the scene is never still.
- Move, sprint, jump and collide. Every frame we turn held keys into a direction, face it the way the player looks, move at walk or sprint speed, apply gravity and push the player out of crates.
- Update the first or third person camera and render. In first person the camera sits in the player head; in third person it follows smoothly from behind.
The detailed code breakdown and the full copy-paste file are below. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.
Because the autopilot writes into the same keys object as the real keyboard, the movement code never needs to know who is driving. That is the same trick games use for AI players and replays.
Frequently Asked Questions
How do you handle keyboard input in Three.js?
Three.js does not have its own keyboard system; you use normal browser events. Listen for keydown and keyup on window, store the held state in an object such as keys[e.code] = true, and read that object inside your requestAnimationFrame loop to move the player.
How do I make WASD movement in a Three.js game?
Each frame compute forward = W minus S and strafe = D minus A, put them in a Vector3, normalize it, rotate it by the player yaw with applyAxisAngle, and add it to the player position times speed times delta time.
Why use e.code instead of e.key?
e.code names the physical key, like KeyW, no matter the keyboard layout or Shift state. e.key gives the typed character, which changes on AZERTY keyboards and when Caps Lock is on, so WASD controls can break.
How do I stop the arrow keys and Space from scrolling the page?
Call e.preventDefault() in your keydown handler for the game keys while the game is active. In this tutorial that only happens when the demo is running, so the page scrolls normally otherwise.
Why does my player keep walking after I switch tabs?
If the tab loses focus while a key is held, the keyup event never arrives. Clear every key on the window blur event so nothing stays stuck down.
How do keyboard games work on phones?
Phones have no keyboard, so add on-screen buttons that set the same keys entries on pointerdown and clear them on pointerup. The movement code stays exactly the same.
Why do I need to sign in to launch the live demo?
The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site.
Play It and Keep Learning
WASD movement, sprint and jump like this drive Super Soldier Battle, a free Three.js browser FPS and Zombie Attack. Browse every Three.js tutorial, read the articles, or search the site.
Need Three.js or WebGL work for your site or game? I take freelance and contract work: contact me.
Play Game