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

时间:2019-10-14 22:29:21

标签: 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():
    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()

go_upgo_down,...函数只是象征性的,我想编写其他代码以将按下的键转换为向量以进行机器学习。

例如,如果我按下键盘上的w,我会期望这样的向量:

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

当我同时按下wa时,我期望这样:

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

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

1 个答案:

答案 0 :(得分:-1)

我不知道您打算如何使用key_check(),但我希望您可以使用cv2.waitKey()

做同样的事情

如果您想使用pynput,则可以

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

     # your loop

     listener.stop()
     listener.join()

listener = pynput.Listener(on_press=on_press, on_release=on_release)
listener.start()

# your loop

listener.stop()
listener.join()
相关问题