def __init__(self,debug=False): self.KeysPressed = "" self.State = StateMachine(['LISTENING','CAPTURINGCODE','PRINTING']) self.Debug = debug self.LastRight = dt.datetime.now()
class PySter(object): def __init__(self,debug=False): self.KeysPressed = "" self.State = StateMachine(['LISTENING','CAPTURINGCODE','PRINTING']) self.Debug = debug self.LastRight = dt.datetime.now() def right_double(self,event): if (dt.datetime.now() - self.LastRight) < dt.timedelta(seconds=1): print "Fire!" self.LastRight = dt.datetime.now() def pressed_chars(self,event): if self.Debug and (self.State != self.State.PRINTING): out = [] out.append(str(event.KeyID)) out.append(str(event.ScanCode)) out.append(str(event.Ascii)) out.append(str(event.flags)) #out = [str(x) for x in (event.KeyID,event.ScanCode,event.Ascii,event.flags)] print "KeyID\tScanCode\tAscii\tflags" print "\t".join(out) if event.Ascii == kb.VK_BACK: #backspace self.KeysPressed = self.KeysPressed[:-1:] elif event.Ascii == 26: #ctrl-z self.KeysPressed = "" else: if event.Ascii: x = chr(event.Ascii) if self.State == self.State.LISTENING: if event.Ascii: if event.Ascii >= 32 and event.Ascii <= 126: if x == '>': self.KeysPressed += x if self.KeysPressed[-3:] == ">>>": if self.Debug: print "Starting to capture" self.State.switch_to(self.State.CAPTURINGCODE) self.KeysPressed = "" else: self.KeysPressed = "" elif self.State == self.State.CAPTURINGCODE: if event.flags == 1: if self.Debug: print type(event.ScanCode) if event.ScanCode == 77: #up or down arrow code = "ans = " + str(self.KeysPressed) if self.Debug: print list(self.KeysPressed) print "{" + code + "}" try: exec(code) bu = chr(kb.VK_BACK) * (len(self.KeysPressed) + 3) self.State.switch_to(self.State.PRINTING) self.NKeysToPrint = len(str(ans)) kb.ghost_write(bu + str(ans)) except SyntaxError: print "*" * 10 #print traceback.print_exc() print "Syntax Error while trying to execute: {}".format(code) except Exception as e: self.KeysPressed = "" print e.message raise self.State.switch_to(self.State.LISTENING) self.KeysPressed = "" elif event.Ascii: if event.Ascii >= 32 and event.Ascii <= 126: self.KeysPressed += x elif self.State == self.State.PRINTING: self.NKeysToPrint -= 1 #if self.Debug: # print "Printing" + str(self.NKeysToPrint) if self.NKeysToPrint <= 0: self.State.switch_to(self.State.LISTENING)