one keyboard snapshot per frame
alreadyYou know the Game value holds the round and that one frame can run more than one simulation step.
hereWhy the keyboard is sampled once per frame into one Input value that every simulation step reads.
Page: one keyboard snapshot per frame
Job: explanation
Level: junior grug (level 1 of 6)
Source lines:
why grug give these 3 tool notes
why grug give the proof why grug give the grill why grug give the why under an answerthis page says why the code has this shape and what a reader must be careful of.
This page teaches that the keyboard is sampled once per frame into one Input value, so every step inside the frame plays the same input.
why
The input reader samples the keys once and returns a snapshot. read_input at input.ts:13#read_input looks at the keys one time and returns a typed object with three flags: left up, left down, restart. main.ts:8#read_input calls it once before advance, and the whole frame runs on what it returned. direction_of at input.ts:21#direction_of folds the two movement flags into one number (up gives minus one, down gives plus one, both together give zero), and update.ts multiplies that number by paddle speed and the fixed step. Input knows nothing about positions; it imports no geometry from the game.
The restart flag rides a different check. The press check at input.ts:17#pressedge fires on the press edge, so holding R clears the score once per press.
what breaks
Reading keys inside step would sample the keyboard again for every catch-up step the accumulator holds. A quick tap could move the paddle two steps on a slow frame and one step on a fast frame, which is the same monitor-dependent behaviour the fixed step exists to remove.
Using the held-key check for restart would clear the score on every frame the key stays down. The separate press check gives one restart per press.
where it fits
main.ts calls read_input before advance. update.ts receives the snapshot and reads no keys. draw.ts never sees it.
try it
Hold W and S together and watch the left paddle stay still. Press R once and watch both scores return to zero. The proof prints the decisive line, input.ts:13#read_input.
Run the source check from the repository root:
grep -n 'export function read_input' input.ts
run grep -n 'export function read_input' input.ts
It prints:
13:export function read_input(keys: KeyReader): Input {
The cited file input.ts ships with this site at the same path. Run the command from the school folder, or open input.ts.
reference: what the snapshot can and cannot say
read_input at input.ts:13#read_input builds the value from exactly left_up, left_down and restart. It cannot carry a key repeat count, a mouse position or a timing: the Input type has no such fields, and read_input builds it from three key reads in one expression.
The two movement flags may both be true, which is why direction_of exists: both held keys cancel to a zero direction at input.ts:21#direction_of, and the paddle stands still for that frame. One flag alone gives the paddle its full speed times the step length.
reference: every connection to this page
This is reference. The next page in the reading order is at the bottom of the page.
learn first: one value holds the round
goes on to: who owns what
flowchart LR A0["one value holds the round"] H["you are here: one keyboard snapshot per frame"] B0["who owns what"] A0 --> H H --> B0
this badge records quiz answers with a difficulty from 1 to 6. it does not measure independence or responsibility.
Useful Resources
Outside material, checked when this school was built.
- Reliable fixed timestep & inputs | Jakub's tech blog article
Shows why input is sampled once per frame and why edge presses need their own check, as our Input type does.
grill: check what you know
Grug asks. Say the answer out loud, then tap. Grug remembers what you miss and asks again in the pop quiz.
the number is a question's difficulty from 1 to 6. a coloured chip marks a question that asks for a change, an effect or a judgment, declared in the quiz row. the rung badge records quiz answers: two right at a level and the levels below it, with a declared transfer question at level 5 or 6. it does not certify independence or responsibility. the questions are level 1 of 6 recall a fact; level 2 of 6 recall a detail; level 3 of 6 trace a path; level 4 of 6 trace a longer path; level 5 of 6 predict or judge; level 6 of 6 judge a transfer.
try first, then show answer
3. Once per drawn frame
main.ts:8#read_input calls the reader once, then passes the snapshot to advance. Several steps can share one snapshot.
try first, then show answer
1. The score clears on every frame the key stays down
The press-edge check at input.ts:17#pressedge fires on the press edge. The held-key check at input.ts:15#UP reads the key on every frame it stays down, so the score would clear again on each frame of the hold instead of once per press.
try first, then show answer
3. Zero
direction_of at input.ts:21#direction_of subtracts for up and adds for down. Both flags cancel to zero, so the paddle stays put.
try first, then show answer
3. The paddle stands still
move_paddles at update.ts:43#keypair multiplies the direction by PADDLE_SPEED and the step. A zero direction makes that product zero, so the paddle stands still for the frame; one held key gives the paddle its full speed times the step length.
try first, then show answer
3
read_input at input.ts:13#read_input builds the value with exactly three flags: left_up, left_down and restart. No other key reaches the snapshot.
try first, then show answer
3. A tap can move the paddle a different number of steps on a slow frame than on a fast one
main.ts:8#read_input reads the keyboard once before advance, and the catch-up loop in update.ts:11#advance spends the accumulator in steps. Reading keys inside those steps would sample the keyboard again for each one. A quick tap can move the paddle two steps on a slow frame and one on a fast frame, which is the monitor-dependent behaviour the fixed step removes.
try first, then show answer
Input
read_input at main.ts:8#read_input returns one Input value before advance, and the loop at update.ts:11#advance passes that same value to every step. A fresh read inside the loop would give the steps different flags. The shared reading is the three-flag Input value built at input.ts:13#read_input.