游戏按键检测的问题

时间:2020-07-27 18:48:55

标签: python python-3.x

我遇到了一个问题。我正在尝试模拟游戏名称Trackmania的按键。 我通过此链接enter link description here获得了Keypressed和KeyReleased函数。当获取诸如S-W-Z-A-Q-S-D之类的键时,它非常有效,但是当我尝试获取UP-DOWN-LEFT-RIGHT箭头时,它不起作用。

import cv2
import time
import numpy as np
from PIL import ImageGrab
from InputHandling import PressKey, ReleaseKey, UP, DOWN, LEFT, RIGHT

def DrawLine(img):
    n_img = cv2.line(img, (400, 640), (400, 420), (255, 0, 0), 5) # Middle Line
    n_img = cv2.line(img, (400, 640), (200, 420), (255, 0, 0), 5) # Left Line
    n_img = cv2.line(img, (400, 640), (600, 420), (255, 0, 0), 5) # Right Line
    return n_img

l_time = time.time()
while(True):
    getScreen = np.array(ImageGrab.grab(bbox=(0, 40, 800, 640)))
    new_screen = DrawLine(getScreen)    
    PressKey(UP)
    time.sleep(3)
    PressKey(UP)
    l_time = time.time()
    new_screen = cv2.cvtColor(new_screen, cv2.COLOR_BGR2RGB)
    cv2.imshow('window', new_screen)

在另一个文件中,我定义了宏

UP = 0x48
DOWN = 0x50
LEFT = 0x4B
RIGHT = 0x4D

我还尝试通过使用pynput.keyboard`

keyboard.press(Key.up)
time.sleep(3)
keyboard.release(Key.up)

但这是同样的问题。

0 个答案:

没有答案