用python模拟Raspbian中的击键
当试图弄清楚如何在按下按钮(利用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。思考?
该错误表明您的控制序列是字符串(即unicode),但Subprocess似乎期望字节。
在功能keypress
,转换序列为字节就像这样:
if isinstance(sequence, str):
sequence = sequence.encode('ascii')
关于字节串很好的书面记录,和Unicode可以在这里找到:https://nedbatchelder.com/text/unipain.html
圣牛!这就像一个魅力,非常感谢!尽管我为左右箭头定义了不同的序列,但它们正在做一些奇怪的事情,但都给了我空间。 –
另外,这是一个很棒的写作,感谢分享! –
我一直在寻找同样的例子,下面是6个按钮我的工作例如:(它有点乱,但我调试行可能会帮助别人太)
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
from Xlib import X
from subprocess import Popen, PIPE
buttons = [18, 23, 24, 25, 8, 7 ]
opcodes = ['next', 'enter', 'alt tab', 'previous', 'spacebar', 'shift' ]
opcodes2 = ['key Left ', 'key Up ', 'key Down ', 'key Right ', 'key Enter ', 'key Shift_L ']
# set the gpio pins
for button in buttons:
GPIO.setup(button, GPIO.IN, GPIO.PUD_UP)
def keypress(sequence):
#if isinstance(sequence, str):
#sequence = sequence.encode('ascii')
p = Popen(['xte', '-x:0'], stdin=PIPE)
p.communicate(input=sequence)
def main():
while True:
button_state = []
for button in buttons:
button_state.append(GPIO.input(button))
for item in range(len(button_state)):
if button_state[item] == 0:
print (opcodes2[item]) #debugging
# send X key for keystroke in (for the given iteration of the loop) opcodes[button.output(index)]
#print (repr(str(opcodes2[item]).encode('ascii')))
keypress(str(opcodes2[item]).encode('ascii'))
print (repr(button_state)) #for debugging
time.sleep(0.3)
del button_state
if __name__ == '__main__':
main()
注意每个命令的opcodes2内的尾随空格(可以是任何字符)。 ascii编码丢弃了我的最后一个字符,所以这是我有点丑陋的解决方法。
请将相关的代码添加到您的问题! –