Reading Keystrokes from a Program


The { KW } (key wait) selection of the INPUT/OUTPUT menu pauses your program to wait for the next key to be pressed. This function can be used only in a program; if used directly from the keyboard, it is ignored.


Key Codes

Each key on the keyboard is assigned a specific code from 0 to 63 (128 to 191 if you press [ 2nd ] before pressing the key). A table of these codes is listed in Appendix C.

Note:

Do not confuse key codes with program instruction codes or character codes. For example, the key code for the [ SIN ] (alpha letter "A") key is 16, but the program instruction code for SIN is 165 (A5 hex) and the character code for "A" is 65 (41 hex).

The Key Wait Function

The key wait function lets your program detect which key has been pressed by placing the code of the key in the numeric display register. You can use this function only as a program instruction. The calculator executes the key wait instruction as follows.
  • First, the instruction waits for a key (or second function of a key) to be pressed.
  • After a key is pressed, the instruction places the code for that key in the numeric display register.
  • Program execution then continues.

Using the Key Wait Function

To include the key wait function in a program:
  1. Press [ I/O ] and select { KW } from the INPUT/OUTPUT menu.
  2. If you are expecting a particular key, use one of the test instructions to compare the code in the numeric display register with the code you are expecting.

Example Program

Write a program that waits for the [ 1 ], [ 2 ], or [ 3 ] key to be pressed, and then executes a specific routine depending on which key was pressed.

PC =

Program Mnemonics

Comments

0000LBL ST
0003 CLRClears calculator
0004 `ROUTINE (1-3)?`Creates message
0018 KWWaits for key
0019 STO AStores key code in A
0021 63 IF= A GTL AAIf "1" executes label AA
0028 57 IF= A GTL BBIf "2" executes label BB
0035 41 IF= A GTL CCIf "3" executes label CC
0042 GTL STOtherwise, repeats prompt
0045LBL AA
0048 `ONE` HLTUser pressed "1"
0052LBL BB
0055 `TWO` HLTUser pressed "2"
0059LBL CC
0062 `THREE` HLTUser pressed "3"


Running the Example

Run the program and press either [ 1 ], [ 2 ], or [ 3 ] key. The program displays the message that corresponds to the key and then halts.


Back