当试图弄清楚如何在按下按钮(利用GPIO管脚)后如何让python脚本发送左或右击键时,我偶然发现了this question。用python模拟Raspbian中的击键
似乎所选的答案是我需要让我去,但安装xautomation后,试图调整代码到我的脚本和阅读Popen的文档(我甚至尝试运行一个新的与逐字的代码)我不断收到以下错误文件:
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
我在皮3 B型运行Python3,在Raspbian杰西使用Pixel(从raspberrypi.org下载)
任何想法为什么它出错了?
如果它有帮助,我所要做的就是允许用户在FEH幻灯片中向左和向右滚动......我可能完全脱离这个方法,给出了这个任务看似简单的任务是。我不是在寻找某个人为我彻底解决这个问题 - 我喜欢与编码相关的挑战 - 我只是超新的python;将我推向正确的方向将会非常有帮助。
任何帮助非常感谢!
编辑:对不起,不包括代码!
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)
编辑编辑:
这里是我更新的代码...它实际打印的左,右按键操作空间。
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)
我已经阅读了关于subprocess and Popen.communicate(),但无法真正判断问题是否有事可做与或有什么xte is expecting as an argument。思考?
请将相关的代码添加到您的问题! –