Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.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 pynput和opencv在linux中检测键盘上的按键组合?_Python_Linux_Opencv_Keypress_Pynput - Fatal编程技术网

如何使用python pynput和opencv在linux中检测键盘上的按键组合?

如何使用python pynput和opencv在linux中检测键盘上的按键组合?,python,linux,opencv,keypress,pynput,Python,Linux,Opencv,Keypress,Pynput,我正在做一个项目,这个项目是关于在Linux平台上创建一个自主驱动程序的。我需要在特定的时间捕捉键盘上按下的键,尤其是当它们同时按下时。我写了这段代码,它在windows中运行得非常好,但在linux中却不太好: import time import cv2 import mss import numpy as np from pynput.keyboard import Key, Listener def up(): print("Go up") def down():

我正在做一个项目,这个项目是关于在Linux平台上创建一个自主驱动程序的。我需要在特定的时间捕捉键盘上按下的键,尤其是当它们同时按下时。我写了这段代码,它在windows中运行得非常好,但在linux中却不太好:

import time
import cv2
import mss
import numpy as np
from pynput.keyboard import Key, Listener

def up():
    print("Go up")


def down():
    print("Go down")


def left():
    print("Go left")


def right():
    print("Go right")


def up_left():
    print("Go up_left")


def up_right():
    print("Go up_right")


def down_left():
    print("Go down_left")


def down_right():
    print("Go down_right")


def do_nothing():
    print("Do Nothing")


# Create a mapping of keys to function (use frozenset as sets are not hashable - so they can't be used as keys)



# The keys combinatons to check

combination_to_function = {
    frozenset([Key.up]): up,
    frozenset([Key.down, ]): down,
    frozenset([Key.left, ]): left,
    frozenset([Key.right, ]): right,
    frozenset([Key.up, Key.left]): up_left,
    frozenset([Key.up, Key.right]): up_right,
    frozenset([Key.down, Key.left]): down_left,
    frozenset([Key.down, Key.right]): down_right,
}

# Currently pressed keys
current_keys = set()

def on_press(key):
    # When a key is pressed, add it to the set we are keeping track of and check if this set is in the dictionary
    current_keys.add(key)
    if frozenset(current_keys) in combination_to_function:
        # If the current set of keys are in the mapping, execute the function
        combination_to_function[frozenset(current_keys)]()


def on_release(key):
    # When a key is released, remove it from the set of keys we are keeping track of
    if key in current_keys:
        current_keys.remove(key)


def process_img(original_img):
    processed_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY)
    processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300)
    return processed_img


with mss.mss() as sct:
    # Part of the screen to capture
    monitor = {"top": 0, "left": 70, "width": 640, "height": 480}

    while True:
        listener = Listener(on_press=on_press, on_release=on_release)
        listener.start()
        last_time = time.time()
        # key_catcher = MockButton()
        # Get raw pixels from the screen, save it to a Numpy array
        screen = np.array(sct.grab(monitor))
        new_screen = process_img(original_img=screen)

        # Display the picture
        cv2.imshow("Window", new_screen)

        # print("Loop took {} seconds".format(time.time() - last_time))
        # Press "q" to quit

        k = cv2.waitKey(10)

        if k & 0xFF == ord("q"):
            cv2.destroyAllWindows()
            break

        listener.stop()
上升,下降。。。函数只是象征性的,我想写其他代码,用于将按下的键转换为机器学习过程中的向量

例如,如果我在键盘上按w键,我希望得到如下向量:

 w   s   a   d   wa  wd  sa  sd  nk
[1   0   0   0   0   0   0   0   0 ]
当我同时按下w和a时,我期望:

 w   s   a   d   wa  wd  sa  sd  nk
[0   0   0   0   1   0   0   0   0 ]

无论如何,代码在linux中运行得不够好。程序运行一段时间后会遇到一些问题。程序运行一段时间后,终端停止输出。有谁能帮我在linux中提高这段代码的效率吗?

我不知道你想用你的密钥检查做什么,但我希望你能用cv2.waitKey做同样的事情

如果您想使用pynput,那么您可以这样做

with Listener(on_press=on_press, on_release=on_release) as listener:

     # your loop

     listener.stop()
     listener.join()


为什么要使用key_检查是否有cv2.waitKey?顺便说一句:keyList=[\b]+listABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789,.£$/\\。它应该适用于\bABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789,.£$/\\:顺便说一句:您不必使用。。。作为侦听器:listener.join。您可以在循环之前运行listener.start,在循环之后运行listener.stop。是否可以检测与cv2.waitKey同时按下的键?例如,按w键时,我希望某个函数运行,同时按w键和A键时,我希望另一个函数运行。因此,捕捉正确的按键很重要。使用cv2.waitKey在按下w键后,您必须等待几毫秒几次循环,如果在下一次循环中获得A,则运行秒函数。若你们并没有得到一个函数,那个么你们运行第一个函数。但你不能同时检查是否没有释放w。对于listener,您可能也需要这样做——您需要等待几个循环来检查您是否没有按A键,但您可以轻松地检查您是否没有释放w。谢谢您的提示。我更新了问题。我需要在loop达到按键检查功能时按下按键。你有什么想法吗?在on_中按use keyList.appendkey和在on_中释放use if key in keyList:keyList.removekey,你将一直按所有键。你只需要把它们分类。或者在按键时使用字典,并在按键时设置按键[a]=True,在按键释放时设置按键[a]=False,
listener = pynput.Listener(on_press=on_press, on_release=on_release)
listener.start()

# your loop

listener.stop()
listener.join()