#!/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 t = Timer(1.0, count) #State machine code def statemachine(): global num global state global exstate global countm global c if(ord(c) == 27): #ESC key num = 0 ''' Add your State Machine code here ''' ''' 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()