[Prev] [Next] [Contents] [Commodore] [New] [Search] [Home]

THE COMMODORE 64 KEYBOARD AND FEATURES

The Operating System has a ton-character keyboard "buffer" that is used to hold incoming keystrokes until they can be processed. This buffer, or queue, holds keystrokes in the order in which they occur so that the first one put into the queue is the first one processed. For example, if a second keystroke occurs before the first can be processed, the second character Is stored in the buffer, while processing of the first character continues. After the program has finished with the first character, the keyboard buffer is examined for more data, and the second keystroke processed. Without this buffer, rapid keyboard input would occasionally drop characters.

In other words, the keyboard buffer allows you to "type-ahead" of the system, which means it can anticipate responses to INPUT prompts or GET statements. As you type on the keys their character values are lined up, single-file (queued) into the buffer to wait for processing in the order the keys were struck. This type-ahead feature can give you an occasional problem where an accidental keystroke causes a program to fetch an incorrect character from the buffer.

Normally, incorrect keystrokes present no problem, since they can be corrected by the CuRSoR-Left <CRSR LEFT> or DELete <INST/DEL> keys and then retyping the character, and the corrections will be processed before a following carriage-return. However, if you press the <RETURN> key, no corrective action is possible, since all characters in the buffer up to and including the carriage-return will be processed before any corrections. This situation can be avoided by using a loop to empty the keyboard buffer before reading an intended response:

   10 GET JUNK$: IF JUNK$ <>"" THEN 10: REM EMPTY THE KEYBOARD BUFFER

In addition to GET and INPUT, the keyboard can also be read using PEEK to fetch from memory location 197 ($00C5) the integer value of the key currently being pressed. If no key Is being held when the PEEK is executed, a value of 64 is returned, The numeric keyboard values, keyboard symbols and character equivalents (CHR$) are shown in Appendix C. The following example loops until a key is pressed then converts the integer to a character value.

   10 AA=PEEK(197): IF AA=64 THEN 10
   20 BB$=CHR$(AA)

[Prev] [Next] [Contents] [Commodore] [New] [Search] [Home]
This page has been created by Sami Rautiainen.
Read the small print. Last updated November 15, 1998.