In the doDebuggingMenu function below, I am getting the user input using the raw_input function (using Python 2.6). This function waits until the user enters a char sequence and presses enter. Neverthless, I would like to allow the user to exit the application by simply pressing the ESC button in the keyboard without having to press enter (Please see the last elif block in the function where I intend to implement this behaviour). For other menu options the user should be able to press the enter button because there are options in the menu that requires entering a two digit number. My question here is how can I provide this combined behaviour in the following function? Thanks
def doDebugingMenu():
while(1):
printDebugingMenu()
char = raw_input("\nPlease, enter your selection in the debugging menu...:")
if char == '1':
doSetTraceLevelManually()
elif char == '2':
doSetTraceDomain()
elif char == '3':
doPrintLevels()
elif char == '4':
doPrintConfig()
elif char == '5':
doPrintProfile()
elif char == '6':
doPrintMap()
elif char == '7':
doPrintCounters()
elif(char == '8'):
doRaiseAlarm()
elif(char == '9'):
doClearAlarm()
elif(char == '10'):
doUpdateAlarm()
elif(char == '11'):
doShutdown()
#HERE I need to catch if ESC pressed
elif char == 'ESC':
break
def printDebugingMenu():
print "\n######################################"
print "# MENU #"
print "######################################"
print "1. setTraceLevel( traceLevel )"
print "2. setTraceDomain()"
print "3. PRINT Trace Domain and levels"
print "4. PRINT config"
print "5. PRINT profile config"
print "6. PRINT mapping config"
print "7. PRINT counters"
print "8. Issue Alarm"
print "9. Remove Alarm"
print "10. Update Alarm"
print "11. Shutdown"
print "EXIT: press ESC"
raw_input()is not what you need.You need to process input character by character so you can detect
ESC, function keys, etc. Maybe build your own input function that returns the text when you press ENTER but throws exceptions on other keys, but then you need to process backspaces, cursor movements, etc. You can find primitives in thecursesmodule on Unices ormsvcrton Windows. It’s a lot of work. But then you have it and it’s reusable if you are writing lots of console based programs.Normally, though, users are quite content to just use
Ctrl-Cif they want to exit the program and there is no need to complicate things. If you want something fancy write a GUI program. Up to you.On a side note, that
if…elif…elifblock looks horribly non-pythonic. Take a look at this.