Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python获得系统范围的按键_Python_Bash_Curses_Xinput - Fatal编程技术网

使用python获得系统范围的按键

使用python获得系统范围的按键,python,bash,curses,xinput,Python,Bash,Curses,Xinput,我正在尝试编写一个程序,每当按下某些键时,该程序就会执行代码。我目前有这个工作,但不幸的是,这个解决方案是非常缓慢的。Python直到按键几秒钟后才知道按键的情况 command = "./STB_KEYCAP.sh" popen = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) for stdout_line in iter(popen.stdout.readline, ""): stdout_line =

我正在尝试编写一个程序,每当按下某些键时,该程序就会执行代码。我目前有这个工作,但不幸的是,这个解决方案是非常缓慢的。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

同样,这确实有效,但需要几秒钟才能采取行动。任何关于如何重写此文件以使其更快的提示都将非常有用

我最终找到了一个对我有效的解决方案。这样做的缺点是,它要求脚本以管理员权限运行,这在我的情况下不是问题,但在某些情况下可能不起作用。 我最终解析了键盘的/dev/input文件,并根据其中的更改确定按键。这个解决方案非常快,并给出了很好的结果

f = open( "/dev/input/event5", "rb" );
while 1:
    data = f.read(24)
    bytes = struct.unpack('4IHHI', data)
    value = str(bytes).split(", ")
    if value[6] == "0)":
        if value[5] != "0":



                # DO SOMETHING WITH KEY

也许是使用信号;如果这太有限了,那么在我使用的Linux上使用如何呢?它有GUI来将Python脚本分配给键组合。它是用Python创建的,因此您可以查看其源代码或将其用作普通程序