Receiving Keypad Events Using Kp.getkey Method
Setting kp.genkpevent= 0- NO allows you to disable the generation of on_kp events and use the keypad in the polled mode.
In this mode, you receive keypad events by calling the kp.getkey method. Here is a modified example from the previous topic.
Typically (but not necessarily), the polled mode is used right inside the on_sys_init event, where an infinite loop is placed to handle keypad input. Notice the doevents statement — it allows other events to execute while the infinite loop is running:
** 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.genkpevent=NO
kp.enabled=YES
dim key_event as pl_kp_event_codes
dim key_code as byte
while 1
if kp.getkey(key_event, key_code)=OK then
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>")
goto skip
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>")
goto skip
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:
goto skip
end select
sys.debugprint("INPUT_STRING> "+inp_str+chr(13))
end if
end if
skip:
doevents
wend
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
You will undoubtedly want to know whether it is better to use the event mode or the polling mode of the kp. object. The answer is, use the event mode whenever possible and convenient. TiOS is the event-driven OS, so events should be the default way of getting things done in your code. This said, there are cases when you will find that it is just better for you to employ the polled mode. Heck, even we didn't know that we needed the polled mode until after we started working on some really sophisticated projects... and then we found out that the polled mode made actually the code architecture better!