我正在做一个有关在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
,...函数只是符号性的,我想编写其他代码以将按下的键转换为 vector 以进行机器学习。例如,如果我在键盘上按
w
,则期望这样的 vector : 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/