#!/usr/bin/env python import thread from threading import Timer import kbhit from kbhit import * num = 15 flag = 0 # Used to detect key hit character exactly once def kbListener(kb): ''' Stores the input from the keyboard into global variable c--> used in thread ''' global num global c global flag while num != 0: if (kb.kbhit()): c = kb.getch().lower() #Get character and change it to lower case flag = 1 #Test patterns #if ord(c) == 97: # a # print ("Pressed \'a\'\n") #if ord(c) == 10: # Newline/Linefeed # print ("Pressed Enter\n") #if ord(c) == 27: # ESC # print ("Success\n") # num = 0 #print(c) def count(): ''' Counts down from 15 to 0. To be used in a separate thread ''' global num num = num-1 if num > 0: print num global t t = Timer(1.0, count) t.start() else: print "Time Out!" return 0 state = 0 exstate = 0 countm = 0 unique = 0 t = Timer(1.0, count) #State machine code def statemachine(): global num global state global exstate global countm global unique global c if(ord(c) == 27): #ESC key num = 0 ''' Add your State Machine code here ''' if state == 0 : if 's' in c: state = 0 exstate = 1 #Could be star or sith elif 'd' in c: state = 1 exstate = 2 #Could be darth elif 't' in c: state = 1 if exstate != 1 and exstate != 5 and exstate != 4: #Could be star or darth or sith exstate = 3 #Could be tarkin elif 'i' in c: if exstate == 3: state = 3 elif exstate == 1: state = 0 exstate = 4 #Could be sith else : state = 0 exstate = 0 elif 'k' in c: state = 0 if exstate != 3: exstate = 0 else : state = 0 exstate = 0 #Substates elif state == 1 : if 'a' in c: state = 2 if exstate == 4 or exstate == 5: exstate = 3 elif 'h' in c: state = 0 #roll back if exstate == 5: if unique != 5: unique = 5 countm = countm+1 #Detected darth elif exstate == 4: if unique != 4: unique = 4 countm = countm+1 #Detected sith exstate = 0 elif 's' in c: state = 0 exstate = 1 #Could be star or sith elif 'd' in c: state = 1 exstate = 2 #Could be darth elif 't' in c: state = 1 exstate = 3 #Could be tarkin else: state = 0 exstate = 0 elif state == 2 : if 'r' in c: state = 0 if exstate == 1: exstate = 3 #continue to tarkin if unique != 1: unique = 1 countm = countm+1 #Detected star elif exstate == 2: #darth exstate = 5 elif exstate != 3: exstate = 0 elif 's' in c: state = 0 exstate = 1 #Could be star or sith elif 'd' in c: state = 1 exstate = 2 #Could be darth elif 't' in c: state = 1 exstate = 3 #Could be tarkin else : state = 0 exstate = 0 elif state == 3 : if 'n' in c: state = 0 # roll back if exstate == 3 : if unique != 3: unique = 3 countm = countm+1 #Detected tarkin exstate = 0 elif 's' in c: state = 0 exstate = 1 #Could be star or sith elif 'd' in c: state = 1 exstate = 2 #Could be darth elif 't' in c: state = 1 exstate = 3 #Could be tarkin else : state = 0 exstate = 0 #Check if we detected two unique keywords if countm == 2: print "Success !" num = 0 ''' End of state machine code ''' return 0 # Code begins here if __name__ == "__main__": kb = KBHit() listen = thread.start_new_thread(kbListener, (kb, )) c = ' ' t.start() while(num != 0): if flag == 1: statemachine() flag = 0 #Reset flag so as to avoid checking the previous character t.cancel()