I stumbled upon this question when trying to figure out how to get a python script to send a left or right keystroke after button presses (utilizing the GPIO pins).
It seems like the chosen answer to the linked question is what I need to get me going but after installing xautomation, trying to adapt the code to my script and reading up on the documentation for Popen (I even tried running a new file with the code verbatim) I keep getting the following error:
Traceback (most recent call last):
File "/home/zdistaging/Documents/xte test.py", line 17, in <module>
keypress(shift_a_sequence)
File "/home/zdistaging/Documents/xte test.py", line 15, in keypress
p.communicate(input=sequence)
File "/usr/lib/python3.4/subprocess.py", line 941, in communicate
self.stdin.write(input)
TypeError: 'str' does not support the buffer interface
I'm running Python3 on a Pi 3 model B, in Raspbian Jessie with Pixel (downloaded from raspberrypi.org)
Any ideas why it's erroring out?
If it helps at all, all I'm trying to do is allow a user to scroll left and right in a FEH slideshow... I might be totally off the mark with this approach given how seemingly simple a task this is. I'm not looking for someone to solve this for me outright - I like the challenge associated with coding - I'm just super new to python; nudging me in the right direction would be super helpful.
Any help is greatly appreciated!!!
EDIT: Sorry about not including the code!
from subprocess import Popen, PIPE
control_f4_sequence = '''keydown Control_L
key F4
keyup Control_L
'''
shift_a_sequence = '''keydown Shift_L
key A
keyup Shift_L
'''
def keypress(sequence):
p = Popen(['xte'], stdin=PIPE)
p.communicate(input=sequence)
keypress(shift_a_sequence)
keypress(control_f4_sequence)
EDIT EDIT:
Here's my updated code... it actually prints spaces for both left and right button presses.
import time
import RPi.GPIO as GPIO
from subprocess import Popen, PIPE
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
leftArrow = '''key \x1B[D''' # I've tried '''key Left'''
rightArrow = '''key \x1B[C''' # and '''key Right''' with the same results
offButton = 26 # Black wire
onButton = 19 # White wire
leftButton = 13 # Red wire
rightButton = 6 # Green wire
def keypress(sequence):
if isinstance(sequence, str):
sequence = sequence.encode('ascii')
p = Popen(['xte'], stdin=PIPE)
p.communicate(input=sequence)
GPIO.setup( offButton, GPIO.IN, GPIO.PUD_UP )
GPIO.setup( onButton, GPIO.IN, GPIO.PUD_UP )
GPIO.setup( leftButton, GPIO.IN, GPIO.PUD_UP )
GPIO.setup( rightButton, GPIO.IN, GPIO.PUD_UP )
while True:
offButton_state = GPIO.input( offButton )
onButton_state = GPIO.input( onButton )
leftButton_state = GPIO.input( leftButton )
rightButton_state = GPIO.input( rightButton )
if offButton_state == GPIO.LOW:
print( "Off button pressed" )
if onButton_state == GPIO.LOW:
print( "On button pressed" )
if leftButton_state == GPIO.LOW:
keypress(leftArrow)
print( "Left button pressed" )
if rightButton_state == GPIO.LOW:
keypress(rightArrow)
print( "Right button pressed" )
time.sleep( 1 )
I've read up on subprocess and Popen.communicate() but couldn't really tell if the problem had something to do with that or with what xte is expecting as an argument. Thoughts?