I tried to catch the number of repeating keys to implement press and hold event,but i think there is some error in the code.
Code:
class Keyboard(object):
def __init__(self,onevent=lambda:None):
self._keyboard_state={}
self._downs={}
self._onevent=onevent
def press_hold(self,scancode):
repeat=0
print repeat
while self.pressed(scancode):
if repeat==10:
return True
print repeat
repeat=repeat+1
def handle_event(self,event):
if event['type'] == appuifw.EEventKeyDown:
code1=event['scancode']
if not self.is_down(code1):
self._downs[code1]=self._downs.get(code1,0)+1
self._keyboard_state[code1]=1
elif event['type'] == appuifw.EEventKeyUp:
self._keyboard_state[event['scancode']]=0
self._onevent()
def is_down(self,scancode):
return self._keyboard_state.get(scancode,0)
def pressed(self,scancode):
if self._downs.get(scancode,0):
self._downs[scancode]-=1
return True
return False
Can someone look at it and tell me where am i making a mistake.