Receiving Keypad Events Through On_kp Event
The keypad object supports two modes of receiving keypad events. The mode is defined by the kp.genkpevent property.
If kp.genkpevent= 1- YES (default), the keypad object generates on_kp events. On_kp is fired every 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 key code of that key.
Let's suppose that you have a keypad from the Key Codes topic. Here is a small app that accumulates user input in a string. The string is cleared if the <CANCEL> key is pressed. The keypad is disabled once the <ENTER> key is pressed. To re-enable input, press the MD button.
** Tibbo Basic **
sub on_sys_init()
kp.scanlinesmapping="24,25,26,27"
kp.returnlinesmapping="28,29,30,31"
'this is only necessary on devices with explicit output buffer control
io.num=24
io.enabled=YES
io.num=25
io.enabled=YES
io.num=26
io.enabled=YES
io.num=27
io.enabled=YES
kp.autodisablecodes=str(PL_KP_EVENT_PRESSED)+",49"
kp.enabled=YES
end sub
'----------------------------------------------------------------
sub on_kp(key_event as enum pl_kp_event_codes, key_code as byte)
if key_event=PL_KP_EVENT_PRESSED then
select case key_code
case 1:
inp_str=inp_str+"1"
case 17:
inp_str=inp_str+"2"
case 33:
inp_str=inp_str+"3"
case 49:
sys.debugprint("<ENTER> pressed. Keypad disabled. Press MD to re-enable.\x0D>")
exit sub
case 2:
inp_str=inp_str+"4"
case 18:
inp_str=inp_str+"5"
case 34:
inp_str=inp_str+"6"
case 50:
inp_str="" 'this is the <CANCEL> key
sys.debugprint("<CANCEL> pressed. The string is now empty.\x0D>")
exit sub
case 3:
inp_str=inp_str+"7"
case 19:
inp_str=inp_str+"8"
case 35:
inp_str=inp_str+"9"
case 51:
inp_str=inp_str+"0"
case else:
exit sub
end select
sys.debugprint("INPUT_STRING> "+inp_str+chr(13))
end if
end sub
'----------------------------------------------------------------
sub on_button_pressed()
if kp.enabled=NO then
kp.enabled=YES
inp_str=""
sys.debugprint("Keypad re-enabled.\x0D")
end if
end sub