LOOK AND TYPE / STUDY / LEVEL 3 OF 5
A line of text
is a state.
The words I type must go to the thing I was looking at when I started, not to whatever my head happens to point at three letters later. And typing has corner cases: Enter on an empty line, Backspace down to nothing, Shift, the arrow keys, and the Space+K chord that switches keyboard mode off. The scene handles all of this with one small state and one pure function. The state is the line: what is typed, and onto which thing. The function takes the line, one key event and the current focus, and returns the new line and one word saying what happened. Nothing else is remembered anywhere.
Type onto a thing
your real keyboardMy head points at (this is what levels 1 and 2 decide)
Click here, then type on your keyboard
Or press the keys here
The ring is blue while I only look, and yellow once a line is locked onto a thing. This page has no AI: where the scene would ask it, the page only says so.
The line
the whole stateEvery key so far, newest first
One key
js/scenes/hw9/gazeType.jsRead it from the top: each kind of key returns early. The focus is used in only two places: for the first letter of a line, and for Enter on an empty line.
What the scene does with the answer
js/scenes/hw9/deskTyping.jsThe function decides nothing about the AI or about notes. The scene reads did and acts. A commit on an empty place always asks the AI to build. A commit on a thing asks the AI to change it, unless Shift was held: then the text is added to the thing's notes, at most four lines. Enter on an empty line removes the newest note.
What you should be able to explain
- The lock. Point at the house and type one letter. Now point at the cup and go on typing. The line stays on the house, because
targetis taken from the focus only whiletextis empty. - How the lock ends. Enter and Escape both return a fresh empty line. Backspace down to no text sets
targetback to null, so the next letter locks onto whatever is in focus then. - No focus, no typing. Choose "nothing" and type. The line does not change and
didis'nofocus'. The scene then shows a hint to look at something first. - The chord. Keyboard mode is left by holding Space and tapping K. Both keys reach
lineKeyas ordinary keys before the mode changes. So a Space remembers the line from before it went down. If K comes while Space is still held, the line goes back to that. Without this, leaving keyboard mode would leave " k" at the end of the line. - Why a pure function. The whole behaviour can be tested by feeding it keys, with no browser and no headset. This page does the same thing with your keyboard.
Exercises: ① Type "ab", press Backspace twice, point at another thing and type "c". Which thing gets the "c", and which line of lineKey decided it? ② Type a word onto the cup and press Shift+Enter three times with different words, then press Enter on an empty line. What happens to the notes? ③ Type "a", then use the chord button. Compare the state before and after. Then hold your real Space key down a little longer: why does repeat matter?