|
Servicing Keypad Events |
Top Previous Next |
Once you have correctly preset and enabled the kp. object, you only need to process keypad events.
The on_kp is the main event that is generated each time a key transitions to a new key state. The key_event argument will tell you what that new state is, while the key_code will tell you the code of the key. Event codes are defined in the on_kp_event_codes enum. The key code is composed of the scan line number (bits 7-4 of the key code), and the return line number (bits 3-0). Scan and return lines are numbered in the same order they are listed in the kp.scanlinesmapping and kp.returnlinesmapping properties.
For example, supposing you have a 4x4 keypad with <0> - <9> keys, also <F1> - <F4>, <E> (enter) and <C> (cancel). Notice how key codes in their hex representation reflect the number of the scan line (high digit) and return line (low digit):

Set up the kp. object to work correctly with your hardware:
kp.scanlinesmapping="24,25,26,27" kp.returnlinesmapping="28,29,30,31"
io.num=PL_IO_NUM_24 io.enabled=YES io.num=PL_IO_NUM_25 io.enabled=YES io.num=PL_IO_NUM_26 io.enabled=YES io.num=PL_IO_NUM_27 io.enabled=YES
'we are not going to change default delay values -- we like them as they are (we came up with them, after all)
kp.autodisablecodes=str(PL_KP_EVENT_PRESSED)+",49" '<ENTER> will disable further input
kp.enabled=YES
|
Here is an example of the event handler that adds your input to the inp_str string (global variable), clears the string when the <CANCEL> key is pressed, and launches the mysterious process_it procedure when the <ENTER> key is pressed:
Sub On_kp(key_event As pl_kp_event_codes, key_code As Byte) Dim x As Byte If key_event=PL_KP_EVENT_PRESSED Then Select Case key_code Case &h1: x=1 Case &h11: x=2 Case &h21: x=3 Case &h2: x=4 Case &h12: x=5 Case &h22: x=6 Case &h3: x=7 Case &h13: x=8 Case &h23: x=9 Case &h33: x=0 Case &h32: '<CANCEL> inp_str="" Exit Sub Case &h31: '<ENTER> will disable the keypad... process_it() inp_str="" kp.enabled=YES '... so we re-enable it Exit Sub End Select inp_str=inp_str+chr(x) 'we will be here only when a numerical key is pressed (see 'exit sub' under CANCEL and ENTER keys) End If End Sub
|
Handling keypad buffer overflows
Another event -- on_kp_overflow -- tells you that the input buffer of the keypad has been overwhelmed with frantic user input and the kp. object is not disabled. You respond appropriately:
Sub On_kp_overflow lcd.print("WHAT'S THE RUSH? SLOW DOWN!",0,0) 'tell the user inp_str="" 'clear the string kp.enabled=YES 're-enable the keypad End Sub
|