Skip to content

Instantly share code, notes, and snippets.

@Robotto
Created January 22, 2020 07:52
Show Gist options
  • Save Robotto/7b2c11c13ae155fd25b541e5b1edfeb5 to your computer and use it in GitHub Desktop.
Save Robotto/7b2c11c13ae155fd25b541e5b1edfeb5 to your computer and use it in GitHub Desktop.
Emulate keyboard from GPIO
#buttonRefresh.py
#by robotto - november 2016
#reads a button on a pi and executes a call to xte with
# a key sequence.
# sudo apt-get install python-rpi-gpio python3-rpi.gpio
from subprocess import Popen, PIPE #to call xte
import RPi.GPIO as GPIO #for button
from time import sleep #for debounce
#BCM pin numbering ( look at https://pinout.xyz/ )
GPIO.setmode(GPIO.BCM)
#Pin is set to utilize the internal pull-up resistor,
# so a buttonpress will ground the pin
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
try:
while True:
sleep(0.02) #debounce for ~20mS
GPIO.wait_for_edge(24, GPIO.FALLING)
print('Button Pressed')
keypress('''key F5''') ##MAGIC HAPPENS HERE
sleep(0.02) #debounce for ~20mS
GPIO.wait_for_edge(24, GPIO.RISING)
print('Button Released')
#cleanup:
except KeyboardInterrupt:
pass
GPIO.cleanup()
def keypress(sequence):
p = Popen(['xte'], stdin=PIPE)
p.communicate(input=sequence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment