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:- Press [ I/O ] and select { KW } from the INPUT/OUTPUT menu.
- 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 |
---|---|---|
0000 | LBL ST | |
0003 | CLR | Clears calculator |
0004 | `ROUTINE (1-3)?` | Creates message |
0018 | KW | Waits for key |
0019 | STO A | Stores key code in A |
0021 | 63 IF= A GTL AA | If "1" executes label AA |
0028 | 57 IF= A GTL BB | If "2" executes label BB |
0035 | 41 IF= A GTL CC | If "3" executes label CC |
0042 | GTL ST | Otherwise, repeats prompt |
0045 | LBL AA | |
0048 | `ONE` HLT | User pressed "1" |
0052 | LBL BB | |
0055 | `TWO` HLT | User pressed "2" |
0059 | LBL CC | |
0062 | `THREE` HLT | User 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