【发布时间】:2019-06-27 15:48:51
【问题描述】:
我正在尝试编写一个在按下某些键时执行代码的程序。我目前有这个工作,但不幸的是,这个解决方案非常慢。 Python 直到按键几秒钟后才发现按键。
command = "./STB_KEYCAP.sh"
popen = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
for stdout_line in iter(popen.stdout.readline, ""):
stdout_line = stdout_line.decode("utf-8")[0]
if stdout_line == "a":
channelUp()
elif stdout_line == "d":
channelDown()
STB_KEYCAP.py:
xinput test-xi2 --root 3| grep -A2 --line-buffered RawKeyRelease | while read -r line ;
do
#Trim line down and remove non digits
if [[ $line == *"detail"* ]];
then
key=$( echo $line | sed "s/[^0-9]*//g")
if [[ $key == "38" ]];
then
echo "a"
fi
if [[ $key == "40" ]];
then
echo "d"
fi
if [[ $key == "42" ]];
then
echo "g"
fi
sleep 0
fi
done
同样,这确实有效,但需要几秒钟才能采取行动。任何关于如何重写它以使其更快的提示都会很棒!
【问题讨论】:
-
也许使用信号;如果这太有限,使用pypi.org/project/keyboard
-
在 Linux 上,我使用 AutoKey,它具有 GUI 来将 Python 脚本分配给键组合。它是用 Python 创建的,因此您可以查看其源代码或将其用作普通程序