Tutorial 8
Using Getkey to detect Key presses

Overview
    This will show you how to use the _getkey ROM call

What is _Getkey?
    _Getkey is a ROM call similar to it's TI-Basic counterpart command, getkey. Getkey waits for a key press from the key pad. Then, depending on what you want to do, getkey can display the key code of the key pressed, execute another command, or act like a program pause.

Programming
   
This program will use _getkey as a program pause.

#define B_CALL(xxxx)   rst 28h \ .dw xxxx
#define B_JUMP(xxxx)  call 50h \ .dw xxxx

_clrlcdfull                      =4540h             ;The usual constants and defines, look at the New Commands section for more information

_homeup                      =4558h
_getkey                        =4972h

            .org      9D95h

            B_CALL(_homeup)                    ;Call up home screen
            B_CALL(_getkey)                      ;Pause until next key press
            B_CALL(_clrlcdfull)                   ;After key press, clear screen. Or you can use any other clear screen command (Tutorial 3)
            ret                                           ;Return to being a good old calculator

.end
END

New Commands
    _getkey - Detect key press, store key code if necessary

 

Programming
    We will make our own program that displays the Hex key code of the key pressed. The code for the program is here. It looks complicated, but it really isn't. I'll comment the program along the way so you don't get lost. An important concept is the "stack". The stack holds numbers for future use or reference. In this program, it holds the hex code of each key pressed.

 

#define B_CALL(xxxx)   rst 28h \ .dw xxxx
#define B_JUMP(xxxx)  call 50h \ .dw xxxx

_clrlcdf                         =4540h             ;The usual constants and defines, look at the New Commands section for more information
_homeup                      =4558h
_puts                           =450Ah
_putc                           =4504h
_getkey                        =4972h
_newline                       =452Eh

            .org      9D95h

            B_CALL(_clrlcdf)                       ; clear screen
            B_CALL(_homeup)                    ; home the cursor
            ld         hl,txt1                          ; Display the message at the txt1 label
            B_CALL(_puts)
            B_CALL(_getkey)                      ; Detect key press
            push     af                                 ; Save key press in register a
            B_CALL(_clrlcdf)                       ; clear screen again
            B_CALL(_homeup)                    ; home again
            ld         hl,txt2                          ; Display msg2
            B_CALL(_puts)                          ; Display text
            pop       af                                 ; restore reg a
            call       dishex                          ; Follow commands at label dishex to find hex key code
            B_CALL(_getkey)                      ; pause until next key press
            B_CALL(_newline)                     ; Move cursor to next line
            ret                                           ;Return to TI-OS

dishex: push     af                                 ; keep copy of value
            and       0F0h                            ; terminate lower nibble
            rra                                           ; shift nibble right 4
            rra
            rra
            rra
            ld         l,a                                ; Display the Key's hex code in Str1
            ld         h,0
            ld         de,hexStr
            push     de                                ; keep for other nibbler
            add       hl,de                            ; add offset to hexStr address
            ld         a,(hl)                            ; find correct ASCII character from string below to display hex code of key pressed.

bcall(_putc)                                          ; display character
            pop       de
            pop       af
            and       0Fh                              ; wipeout upper nibble
            ld         l,a
            ld         h,0
            add       hl,de
            ld         a,(hl)
            bcall(_putc)
            ret                                           ; Return to OS

hexStr .db       "0123456789ABCDEF"  ;These are the characters used to display the key's hex code
txt1      .db       "Push a key.",0
txt2      .db       "Key code: ",0

.end
END

New Commands
         _putc - Display character and advance cursor
         push - Pushes a number on to the stack.
         pop - Takes a number off the stack.
         call - This looks like the TI-83 macro, and serves almost the same purpose, only for the TI-83 Plus, it jumps the program to a specified label.

Conclusion
    This program wasn't exactly "lightning fast". That's because the _Getkey command was scanning the entire key pad for a key press. Later on, we will make a program like this one, only we'll narrow down the area in which _Getkey looks in for a keypress, making it faster.

 

Tutorial 9

    Click to return to the site's menu... or here to get back to the tutorial's menu.