0

I have a python application being used via console on raspberry pi. It run on startup from the rc.local script. The script does something when a GPIO button is pressed, however I also want to have exit when/if the user hits escape. I've tried things with threads, raw_input, etc but cant get it to work. Here is the script now:

#!/usr/bin/python

import RPi.GPIO as GPIO, time, os, subprocess, dropbox, signal, sys, threading
os.system("clear")

def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

# GPIO setup


print("Welcome to the PhotoBooth")
while True:
 try:
  if (GPIO.input(SWITCH)==False):
    os.system("clear")
    #DO Stuff
 except KeyboardInterrupt:
    print "Bye"
    sys.exit()

How can detect if escape is pressed without requiring a key to be pressed (e.g. raw_input())?

Also - on a related note, using CTRL+C to stop the app works when I run the app manually from the command line but not when it is started from rc.local - hence why I'm looking for the ESC solution.

tvfoodmaps
  • 347
  • 1
  • 3
  • 14
  • Just to clarify, you want to detect if esc is already in the pressed state? Is that what you mean by "without requiring a key to be pressed"? – CrazyCasta Jul 25 '15 at 15:26
  • I'm not sure you can without using something like `raw_input`, unless you want to use something like curses https://docs.python.org/2/library/curses.html – Addison Jul 25 '15 at 15:27
  • See [this answer](http://stackoverflow.com/a/10079805/4014959) in [Python nonblocking console input](http://stackoverflow.com/q/2408560/4014959), or [python non-blocking non-messing-my-tty key press detection](http://stackoverflow.com/q/24582491/4014959) – PM 2Ring Jul 25 '15 at 15:54
  • 1
    I take it that "without requiring a key to be pressed" means you want to detect escape without requiring an Enter at the end of the input? – J Richard Snape Jul 25 '15 at 15:58
  • I think the answers in the two linked questions above contain all you need - although you may need to mix and match to suit your use case. – J Richard Snape Jul 25 '15 at 16:04

0 Answers0