You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

esc-button.py 615B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python
  2. from RPi import GPIO
  3. import time
  4. from subprocess import call
  5. GPIO.setmode(GPIO.BCM)
  6. GPIO.setup(3, GPIO.IN)
  7. pressed = False
  8. shutdown = 2.5
  9. press_start = time.time()
  10. while True:
  11. current = not GPIO.input(3)
  12. if current and not pressed:
  13. call(["xdotool", "keydown", "Escape"])
  14. pressed = True
  15. press_start = time.time()
  16. elif not current and pressed:
  17. call(["xdotool", "keyup", "Escape"])
  18. pressed = False
  19. if current:
  20. if time.time() - shutdown >= press_start:
  21. call(["sudo", "shutdown", "-h", "now"])
  22. time.sleep(0.1)