NCL 301: Reading the Keyboard

Until now, every value our programs worked with was already in the program.

We wrote the numbers.

We wrote the strings.

The CPU followed our instructions using those values.

Now we'll let the person using the computer provide a value instead.

We'll start with the smallest possible kind of input:

one key.

Reading a key

Pressed keys are kept in a queue until the program reads them.

SYS.KEY reads the oldest key currently waiting in that queue.

Let's try it:

D.TXT "PRESS A KEY: "
D.BLT

SYS.KEY $000

D.TXT $000
D.BLT

Run the program without pressing anything first.

You may see:

PRESS A KEY:

and nothing after it.

That's because SYS.KEY does not wait for a key.

If the keyboard queue contains a key, SYS.KEY places that key in $000.

If the queue is empty, it places an empty string there instead:

""

So when execution reached:

SYS.KEY $000

there may simply have been nothing waiting to read.

Checking repeatedly

We could solve that by checking the keyboard again whenever the queue is empty.

We already know how to repeat work with a loop:

D.TXT "PRESS A KEY: "
D.BLT

:wait
SYS.KEY $000
BSEQ :wait $000 ""

D.TXT $000
D.BLT

Now follow the program.

Execution reaches:

SYS.KEY $000

If no key is waiting:

$000 = ""

Then:

BSEQ :wait $000 ""

takes the branch and checks again.

Still nothing?

It checks again.

And again.

Eventually, you press a key.

Suppose you press:

K

Now:

$000 = "K"

The comparison with "" fails, execution continues, and the Display shows:

PRESS A KEY: K

This works.

But while we're sitting there thinking about which key to press, the CPU is repeatedly asking:

Is there a key yet?

Most of the time, the answer is no.

Waiting instead

For cases where we know we need a key before anything useful can happen, NCL provides another operation:

SYS.AKEY $000

SYS.AKEY reads the keyboard queue just like SYS.KEY.

The difference appears when the queue is empty.

Keys wait in a queue

Key presses are read in the order they arrived.

SYS.KEY and SYS.AKEY both read the oldest key waiting in the keyboard queue.

The difference between them is what happens when that queue is empty.

Operation Key waiting Queue empty
SYS.KEY Returns the oldest key Returns ""
SYS.AKEY Returns the oldest key Waits until a key is available

That lets us replace our polling loop with:

D.TXT "PRESS A KEY: "
D.BLT

SYS.AKEY $000

D.TXT $000
D.BLT

Run it again.

This time, if no key is waiting when execution reaches:

SYS.AKEY $000

execution waits there.

Press a key, and the operation completes.

The key is placed in $000, and execution continues.

If a key was already waiting in the keyboard queue, SYS.AKEY would return it immediately.

Polling and blocking

SYS.KEY is a non-blocking operation: if no key is available, it returns immediately.

SYS.AKEY is blocking: if no key is available, the program waits for the operation to complete.

Repeatedly using a non-blocking operation to check whether something has happened is called polling.

These ideas appear throughout real computer systems, not just keyboards.

Following a waiting program

This is a little different from the programs we've traced before.

Consider:

D.TXT "PRESS A KEY: "
D.BLT

SYS.AKEY $000

D.TXT $000
D.BLT

You can still follow execution with your finger.

First:

D.TXT "PRESS A KEY: "

puts the prompt in the Display buffer.

Then:

D.BLT

makes it visible.

Then your finger reaches:

SYS.AKEY $000

and stops.

If no key is waiting, the CPU cannot continue to the next instruction until SYS.AKEY has a key to return.

Imagine the person using the computer presses:

A

Now SYS.AKEY can complete.

$000 contains:

"A"

and your finger can continue to:

D.TXT $000

For the first time, the value our program is working with was chosen by the person using it.

Is the whole computer waiting?

The program is waiting at SYS.AKEY.

That does not mean every part of the computer has stopped doing anything.

The keyboard, Display, Kernel, and other parts of the system can continue doing their own work while the program waits.

From the program's point of view, though, execution remains at SYS.AKEY until the operation completes.

Hit Y to continue

Reading any key is useful, but often we care about one particular key.

Suppose we want:

HIT Y TO CONTINUE

Pressing anything else should keep waiting.

We can combine SYS.AKEY with the string comparisons we already know:

D.TXT "HIT Y TO CONTINUE"
D.BLT

:wait
SYS.AKEY $000
BSNEQ :wait $000 "Y"

D.TXT "\r\nCONTINUING..."
D.BLT

Run it.

If you press:

X

SYS.AKEY places "X" in $000.

The comparison:

BSNEQ :wait $000 "Y"

succeeds, so execution returns to :wait and waits for another key.

If you press:

Y

the comparison fails.

Execution continues:

D.TXT "\r\nCONTINUING..."
D.BLT

and the Display shows:

HIT Y TO CONTINUE
CONTINUING...

The program didn't know when you would press Y.

It simply kept waiting until you did.

What about lowercase y?

Try the program again and press:

y

Nothing happens.

Our comparison is case-sensitive:

BSNEQ :wait $000 "Y"

and "y" is not the same string as "Y".

But the keyboard gave us a string, and we already know how to normalize strings before comparing them.

Add:

S.UPR $000 $000

after reading the key:

D.TXT "HIT Y TO CONTINUE"
D.BLT

:wait
SYS.AKEY $000
S.UPR $000 $000
BSNEQ :wait $000 "Y"

D.TXT "\r\nCONTINUING..."
D.BLT

Now either Y or y becomes:

"Y"

before the comparison.

The same string operations we've already learned work on keyboard input normally.

Confirm? (Y/N)

Waiting for one particular key is useful.

Now let's allow two meaningful choices:

CONFIRM? (Y/N)

We want:

  • Y to confirm;
  • N to cancel;
  • anything else to keep waiting.

Start with the prompt:

D.TXT "CONFIRM? (Y/N) "
D.BLT

Then wait for a key and normalize it:

:input
SYS.AKEY $000
S.UPR $000 $000

Now we can choose between the possible values.

If the key is Y, jump to :yes:

BSEQ :yes $000 "Y"

If it isn't N either, go back and wait for another key:

BSNEQ input $000 "N"

If execution continues past that comparison, the key must have been N.

So the cancellation path can simply fall through:

D.TXT "\r\nCANCELLED"
D.BLT
JUMP 0

Then add the confirmation path:

:yes
D.TXT "\r\nCONFIRMED"
D.BLT

The complete program is:

D.TXT "CONFIRM? (Y/N) "
D.BLT

input
SYS.AKEY $000
S.UPR $000 $000

BSEQ :yes $000 "Y"
BSNEQ input $000 "N"

D.TXT "\r\nCANCELLED"
D.BLT
JUMP 0

:yes
D.TXT "\r\nCONFIRMED"
D.BLT

Run it and try several keys.

Press something other than Y or N.

The program simply waits for another key.

Press:

n

S.UPR turns it into "N".

The BSEQ for "Y" fails, but so does:

BSNEQ input $000 "N"

Execution therefore falls through to:

CANCELLED

Run it again and press y.

This time execution takes the :yes branch and displays:

CONFIRMED

We've built this kind of multi-path choice before.

The important difference is where the value came from.

Earlier, our programs usually made decisions using values already stored or calculated by the program.

Now the person using the computer can decide which path the program takes.

SYS.KEY or SYS.AKEY?

Both operations read the oldest key waiting in the keyboard queue.

The question is what your program should do when no key is available.

Use:

SYS.KEY <dst>

when checking the keyboard is only one thing your program wants to do, and continuing without a key is acceptable.

Use:

SYS.AKEY <dst>

when the program cannot continue usefully until a key arrives.

In this lesson, our prompts require an answer before anything else should happen, so SYS.AKEY is the natural choice.

Later, an interactive program may have other work to do while no key is waiting. In that situation, SYS.KEY can be useful because it lets execution continue immediately.

Try it

Write a small menu that asks:

CHOOSE: (P/S/Q)

It should accept three choices:

P

displays:

PLAYING
S

displays:

SETTINGS

and:

Q

displays:

QUITTING

Lowercase letters should work too.

Any other key should be ignored, and the program should wait for another choice.

You'll need the same ideas we've been using:

  1. wait for a key;
  2. normalize it;
  3. compare it with the accepted choices;
  4. choose a path.

This time there are three accepted choices instead of two, so you'll need to decide how to arrange the branches yourself.


A single key is enough for confirmations, menu choices, controls, and simple commands.

But what if we want the person using the computer to type something like:

EMMA

The keyboard still gives us input one key at a time.

In the next lesson, NCL 302: Building User Input, we'll use the string operations from the 200-level to collect those individual keys into a complete piece of text.