我正在从事一个关于在 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_up
、go_down
... 函数只是象征性的,我想编写其他代码来将按下的键转换为用于机器学习过程的向量。
例如,如果我在键盘上按 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 中的效率??
最佳答案
我不知道你想用你的 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()
关于python - 如何使用 python pynput 和 opencv 检测 linux 键盘上的按键组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384816/