Peripheral myKeypad As KeypadA @ 0 'Set module number to 0 Dim PressKeyD As Byte 'Store the event whether D is pressed Dim KeyStatus As Byte 'Determine whether a key press value is obtained Dim KeyID As Byte 'Store the obtained key press value Dim RepeatTime As Byte 'Store the obtained value of repetition time Dim RepeatRate As Byte 'Store the obtained value of repetition rate Dim DebounceTime As Byte 'Store the obtained value of the debounce time Dim RepeatCount As Byte 'Store the number of repeated key presses. Dim CustomTable(15) As Byte 'Array for storing the user defined key press values Dim i As Byte 'Store the loop countsSub Main() Debug CLS KEYID_CHECK: 'The following loop will be repeatedly executed. According to the pressed key determined by KeyStatus, 'the input key press messages will be shown in the Terminal Window. 'If the pressed key is determined to be ¡§D¡¨, the loop will be exited. 'This part is used for determining the key press value by using Event. myKeypad.SetKeypadmode(0) 'Set the keypad mode to 0, the default mode Pause 100 myKeypad.GetKeyID(KeyID) 'Get the initial key status RepeatCount=0 KeyStatus=0 myKeypad.EnableKeyPressedEvent() 'Enable the Pressed event Debug "Press D To Exit the loop.", CR PressKeyD=0 Do Loop Until PressKeyD>0 REPEAT_CHECK: myKeypad.GetRepeatTime(RepeatTime) 'Get the initial RepeatTime set in the system myKeypad.GetRepeatRate(RepeatRate) 'Get the initial RepeatRate set in the system 'Display the initial Repeat Time set in the system Debug "Repeat Time is currently set As ", RepeatTime, " * 10 ms...", CR 'Display the initial Repeat Rate set in the system Debug "Repeat Rate is currently set As ", RepeatRate, " * 10 ms...", CR myKeypad.SetRepeatTime(50) 'Set the RepeatTime as 500 ms myKeypad.SetRepeatRate(2) 'Set the RepeatRate as 20 ms RepeatCount=1 'The following loop will be executed repeatedly. According to the pressed key determined by KeyStatus. 'If the key remains pressed and held for over half a second, the repeated keypad events will be activated 'according To the RepeatTime setting. 'The keypad event will be activated repeatedly every 20 ms according to the RepeatRate setting. Debug "Please press And hold any key", CR Do Loop Until RepeatCount>100 myKeypad.DisableKeyPressedEvent() 'Disable the Pressed event 'Set RepeatTime and RepeatRate as 0 to disable the repeated keypad event while pressing and holding the key myKeypad.SetRepeatTime(0) myKeypad.SetRepeatRate(0) CUSTOM_TABLE: For i=0 To 15 CustomTable(i)=100+i Next myKeypad.SetCustomTable(CustomTable) 'Set the user defined key press values as 100~115 myKeypad.SaveCustomTable(0) 'Store the user defined key press values in Table 0 myKeypad.SetKeypadmode(7) 'Set the keypad mode to 7, the user defined mode Pause 100 myKeypad.GetKeyID(KeyID) KeyStatus=0 'The following loop will be executed repeatedly. According to the pressed key determined by KeyStatus, 'The input key press messages will be shown in the Terminal Window. 'If the pressed key is determined to be ¡§D¡¨, the loop will be exited. 'It can be observed that the returned Key ID will become the user defined key press value 'Here, the status and key value of the pressed key is obtained by polling. There is no keypad event. Debug "Press D To Exit the Loop", CR Do If myKeypad.GetKeyID(KeyID)<>0 Then Select Case KeyID Case 100 : Debug "Press the Button 1! (The returned value is 100)", CR Case 101 : Debug "Press the Button 2! (The returned value is 101)", CR Case 102 : Debug "Press the Button 3! (The returned value is 102)", CR Case 103 : Debug "Press the Button A! (The returned value is 103)", CR Case 104 : Debug "Press the Button 4! (The returned value is 104)", CR Case 105 : Debug "Press the Button 5! (The returned value is 105)", CR Case 106 : Debug "Press the Button 6! (The returned value is 106)", CR Case 107 : Debug "Press the Button B! (The returned value is 107)", CR Case 108 : Debug "Press the Button 7! (The returned value is 108)", CR Case 109 : Debug "Press the Button 8! (The returned value is 109)", CR Case 110 : Debug "Press the Button 9! (The returned value is 110)", CR Case 111 : Debug "Press the Button C! (The returned value is 111)", CR Case 112 : Debug "Press the Button *! (The returned value is 112)", CR Case 113 : Debug "Press the Button 0! (The returned value is 113)", CR Case 114 : Debug "Press the Button #! (The returned value is 114)", CR Case 115 : Debug "Press the Button D! (The returned value is 115)", CR End Select If KeyID=115 Then Goto KEYID_CHECK End IF End IF Loop End Sub Event myKeypad.KeyPressedEvent() KeyStatus=myKeypad.GetKeyID(KeyID) 'Store the obtained key press value in the parameter KeyID If RepeatCount>100 Then Return Elseif RepeatCount>0 Then RepeatCount+=1 Debug "Count the number of key presses ", RepeatCount, CR Elseif RepeatCount=0 Then Select Case KeyID Case 0: Debug "Press the Button 1!", CR Case 1: Debug "Press the Button 2!", CR Case 2: Debug "Press the Button 3!", CR Case 3: Debug "Press the Button A!", CR Case 4: Debug "Press the Button 4!", CR Case 5: Debug "Press the Button 5!", CR Case 6: Debug "Press the Button 6!", CR Case 7: Debug "Press the Button B!", CR Case 8: Debug "Press the Button 7!", CR Case 9: Debug "Press the Button 8!", CR Case 10: Debug "Press the Button 9!", CR Case 11: Debug "Press the Button C!", CR Case 12: Debug "Press the Button *!", CR Case 13: Debug "Press the Button 0!", CR Case 14: Debug "Press the Button #!", CR Case 15: Debug "Press the Button D!", CR : PressKeyD=1 End Select End IF End Event