NCL 302: Building User Input
In the last lesson, we learned how to read individual keys from the keyboard.
That works well for choices like:
CONFIRM? (Y/N)
But what if we want the person using the computer to type an entire word?
For example:
NAME> Emma
The keyboard still gives us input one key at a time.
So if we want a complete answer, our program has to build it.
An input buffer
We'll keep two strings:
#buffer $000
#key $001
#key will hold the most recently received keyboard input.
#buffer will hold everything the user has typed so far.
Start with an empty buffer:
S.MOVE #buffer ""
Then show a prompt:
D.CHR "NAME> "
Now we can wait for the first key:
SYS.AKEY #key
Suppose the user presses:
E
Now:
#key = "E"
We want to add that character to the end of the buffer:
S.JOIN #buffer #buffer #key
Now:
#buffer = "E"
Imagine the user presses:
m
and we perform the same operation again:
#buffer = "Em"
Then another m:
#buffer = "Emm"
and finally a:
#buffer = "Emma"
The buffer grows one character at a time.
| Key | #buffer |
|---|---|
E |
"E" |
m |
"Em" |
m |
"Emm" |
a |
"Emma" |
Why is it called a buffer?
A buffer provides some cushioning between two parts of a process that may not move at exactly the same rate.
The word predates computers. Mechanical buffers absorb or soften impacts — railway buffer stops at the end of a track are a particularly literal example.
The same idea appears in manufacturing. An assembly line might have a buffer where parts wait between stages. If one stage briefly works faster than the next, the parts have somewhere to go instead of requiring both stages to operate in perfect lockstep.
A computer buffer does something similar with data. It temporarily holds data so that one part of a system does not have to produce it at exactly the same rate that another part consumes it.
You've probably encountered this while streaming video. A player downloads some video into a buffer ahead of playback. If the network briefly delivers data more slowly than the video is being played, playback can continue using the data already waiting in the buffer.
Our #buffer serves the same general purpose. Keyboard input arrives one key at a time, while our program ultimately wants a complete line of text. The buffer gives those individual inputs somewhere to accumulate until the line is ready.
Buffers appear throughout computer systems: displays, files, networks, printers, and many other devices use them to accommodate differences in how and when data is produced and consumed.
Echoing characters
We also want the person typing to see each character as it arrives.
We could write every key using:
D.TXT #key
D.BLT
but that would refresh the Display after every keypress.
For individual characters that should appear immediately, we can write directly to the visible Display:
D.CHR #key
So our input loop can now look like this:
:input
SYS.AKEY #key
S.JOIN #buffer #buffer #key
D.CHR #key
JUMP :input
If the user types:
Emma
the Display shows:
NAME> Emma
while #buffer contains:
"Emma"
Direct and buffered output
D.CHR writes directly to the visible Display.
D.TXT writes to the Display buffer, where several changes can be prepared before D.BLT makes them visible.
For individual characters that should appear immediately, D.CHR is convenient. For larger updates, buffered output is usually a better fit.
We'll use both approaches in this lesson.
Not every key is text
There's a problem with our loop.
SYS.AKEY always gives us a string, but not every string it returns represents text that should be added to our input buffer.
An ordinary character might arrive as:
"A"
or:
"Â"
Keys such as Enter and Backspace are represented by function strings instead:
"ENTER"
"BS"
We don't want either of those strings appearing in the name.
Fortunately, the Keyboard gives us a simple way to distinguish them.
Characters and functions
The Keyboard returns ordinary character input as a string exactly one character long.
Functions and other keyboard commands use strings two or more characters long.
A character does not have to be ASCII. For example, "Â" is still one character long.
We can therefore measure the input:
S.LEN $002 #key
If its length is greater than one, it's a function:
:input
SYS.AKEY #key
S.LEN $002 #key
BGT :command $002 1
S.JOIN #buffer #buffer #key
D.CHR #key
JUMP :input
:command
Ordinary characters are added to the buffer and echoed to the Display.
Functions take a different path.
Handling Enter
The first function we care about is Enter.
When the user presses Enter, we want to stop collecting characters and display a greeting using the completed name.
Add:
:command
S.BEQ :enter #key "ENTER"
JUMP :input
Any function we don't recognize simply returns to :input.
Enter jumps to:
:enter
At this point, we're writing a complete message rather than echoing a single character, so we'll use the Display buffer:
:enter
D.TXT "\r\nHELLO, "
D.TXT #buffer
D.TXT "!"
D.BLT
If the user typed:
NAME> Emma
the finished output becomes:
NAME> Emma
HELLO, Emma!
Our program can now collect a complete line of text.
But there's still one obvious problem.
What happens when the user makes a mistake?
Backspace
Suppose the buffer contains:
"Emma"
and the Display shows:
NAME> Emma
If the user presses Backspace, we need to change two things:
- remove the final character from
#buffer; - remove the visible character from the Display.
First, recognize the function:
:command
S.BEQ :enter #key "ENTER"
S.BEQ :backspace #key "BS"
JUMP :input
Now add the Backspace path:
:backspace
Before deleting anything, we should check whether the buffer is already empty:
S.LEN $002 #buffer
BEQ :input $002 0
If its length is zero, there's nothing to remove.
Otherwise, we can delete the final character directly:
S.DEL #buffer #buffer -1 -1
Remember that -1 refers to the final character of a string.
So:
"Emma"
becomes:
"Emm"
and:
"E"
becomes:
""
There is no special case for the final remaining character. We're simply deleting the last character that exists.
Our stored input is now correct.
We also need to fix what the user sees.
Removing the visible character
In NCL 208, we used:
\b
to move the Display cursor backward one character.
We also used:
\d
to blank the character at the cursor without moving it.
Together:
D.CHR "\b\d"
does exactly what we need.
Suppose the Display currently shows:
NAME> Emma
with the cursor immediately after the final a.
\b moves the cursor back onto that character.
\d blanks it.
The cursor remains in the position where the next typed character should appear.
Our complete Backspace path is therefore:
:backspace
S.LEN $002 #buffer
BEQ :input $002 0
S.DEL #buffer #buffer -1 -1
D.CHR "\b\d"
JUMP :input
Backspace changes two things
S.DEL changes what the program has stored.
D.CHR "\b\d" changes what the user sees.
A working Backspace needs to do both.
Put it together
Here is the complete program:
#buffer $000
#key $001
S.MOVE #buffer ""
D.CHR "NAME> "
:input
SYS.AKEY #key
S.LEN $002 #key
BGT :command $002 1
S.JOIN #buffer #buffer #key
D.CHR #key
JUMP :input
:command
S.BEQ :enter #key "ENTER"
S.BEQ :backspace #key "BS"
JUMP :input
:backspace
S.LEN $002 #buffer
BEQ :input $002 0
S.DEL #buffer #buffer -1 -1
D.CHR "\b\d"
JUMP :input
:enter
D.TXT "\r\nHELLO, "
D.TXT #buffer
D.TXT "!"
D.BLT
Run it.
Type a name, make a mistake, correct it with Backspace, and then press Enter.
Follow the state
Let's trace a short input.
Suppose the user types:
A
d
a
BS
m
ENTER
The important state changes look like this:
| Input | #key |
#buffer |
Action |
|---|---|---|---|
A |
"A" |
"A" |
Append and echo |
d |
"d" |
"Ad" |
Append and echo |
a |
"a" |
"Ada" |
Append and echo |
BS |
"BS" |
"Ad" |
Remove final character |
m |
"m" |
"Adm" |
Append and echo |
ENTER |
"ENTER" |
"Adm" |
Finish input |
#key describes what just happened.
#buffer describes what the user has entered so far.
That distinction lets a stream of individual keyboard inputs become one useful string.
Stored state and visible state
While the user is typing, our program is maintaining the same information in two places.
The actual input is stored in:
#buffer
The user sees a representation of that input on the Display.
When a normal character arrives, we update both:
S.JOIN #buffer #buffer #key
D.CHR #key
When Backspace arrives, we update both again:
S.DEL #buffer #buffer -1 -1
D.CHR "\b\d"
Keep stored and visible state in agreement
#buffer is the program's actual input.
The text on the Display is the user's representation of that input.
When the input changes, both need to be updated. Otherwise what the program knows and what the user sees can disagree.
This is an important idea in interactive programs.
What the computer knows and what it shows the user should agree.
Try it
Modify the program so that the user can enter a city:
CITY>
When Enter is pressed, display:
YOU ENTERED: Montreal
using whatever text the user typed.
Then give the input a maximum length of 12 characters.
Once #buffer contains 12 characters, additional ordinary characters should be ignored.
Backspace should still work.
Enter should still finish the input.
Other functions should still be ignored.
Be careful: if a character is rejected because the buffer is full, it should neither be stored nor displayed.
Test a few edge cases:
- press Backspace while the input is empty;
- type one character and erase it;
- fill all 12 positions and try typing another character;
- erase a character from a full input and type another;
- erase everything and type something new;
- press other function keys and confirm that they are ignored.
We've now turned individual keyboard events into a complete editable string.
Along the way, we've relied on several details of the Display: direct character writes, cursor movement, deleting visible characters, and buffered updates.
We've been using the Display since NCL 101.
In the next lesson, NCL 303: The Display, we'll take a closer look at how it actually works.