如何使用键盘和鼠标Python模块将用户输入(键盘、鼠标)正确存储在文件中?

2022-04-09 00:00:00 python multithreading keyboard mouse

问题描述

我使用键盘和鼠标模块来记录用户与我们软件的交互,以便我们可以进行一些高级的图形用户界面测试。 当前我正在尝试将录制的事件存储在文本文件中,然后再次播放此录制。

但是,当我从所述文件加载记录的事件时,我只看到已播放的mouse事件和无keyboard事件。

此问题的一个原因可能是KeyboardEvents的实现。 KeyboardEvents不包含__repr__的正确实现。这阻止了我们调用print(keyboard_events, file=f)并使用eval(line)读取行。(这适用于鼠标和按钮事件和移动事件) 因此,我们决定使用KeyboardEvents的json格式。基本上,我们要做的是检索每个KeyboardEvent的json格式,并将json写入文件中。然后加载json文件并将json解析为KeyboardEvents。

目前,我们将鼠标和键盘输入存储在单个文件中。但是,由于mouse支持__repr__的正确实现,我们可以直接打印和鼠标事件,并在其上使用eval()来检索存储的事件。

这是用于录制和播放的文件:

import threading
import mouse
import keyboard

from mouse import ButtonEvent
from mouse import MoveEvent
from mouse import WheelEvent

from keyboard import KeyboardEvent

import time

import json
import sys

def record(file='record.txt'):
    f = open(file, 'w+')
    mouse_events = []
    keyboard_events = []
    keyboard.start_recording()
    starttime = time.time()
    mouse.hook(mouse_events.append)
    keyboard.wait('esc')
    keyboard_events = keyboard.stop_recording()
    mouse.unhook(mouse_events.append)
    #first line = start of recording
    #mouse events = second line
    #keyboard events = every remaining line = 1 event
    print(starttime, file=f)
    print(mouse_events, file=f)
    for kevent in range(0, len(keyboard_events)):
        print(keyboard_events[kevent].to_json(), file = f)
    f.close()

def play(file, speed = 0.5):
    f = open(file, 'r')
    #per definition the first line is mouse events and the rest is keyboard events
    lines = f.readlines()
    f.close()
    mouse_events = eval(lines[1])
    keyboard_events = []
    for index in range(2,len(lines)):
        keyboard_events.append(keyboard.KeyboardEvent(**json.loads(lines[index])))

    starttime = float(lines[0])
    keyboard_time_interval = keyboard_events[0].time - starttime
    keyboard_time_interval /= speed
    mouse_time_interval = mouse_events[0].time - starttime
    mouse_time_interval /= speed
    print(keyboard_time_interval)
    print(mouse_time_interval)
    #Keyboard threadings:
    k_thread = threading.Thread(target = lambda : time.sleep(keyboard_time_interval) == keyboard.play(keyboard_events, speed_factor=speed) )
    #Mouse threadings:
    m_thread = threading.Thread(target = lambda : time.sleep(mouse_time_interval) == mouse.play(mouse_events, speed_factor=speed))
    #start threads
    m_thread.start()
    k_thread.start()
    #waiting for both threadings to be completed
    k_thread.join() 
    m_thread.join()


if __name__ == '__main__':
    if len(sys.argv) > 2 and sys.argv[1] == 'play':
        play(sys.argv[2])
    elif len(sys.argv) >= 2 and sys.argv[1] == 'record':
        if(len(sys.argv)) == 3:
            record(sys.argv[2])
        else:
            record()
    else:
        print("missing either 'play' or 'record' or filename")

我期望此代码的行为与它在单个函数中运行时的行为相同(请参阅https://stackoverflow.com/a/57670484/7345513中的编辑)。

含义:我希望线程中的回放能够同步,按键也会被按下。我实际得到的是,鼠标事件按预期回放,但没有处理任何KeyboardEvents。当我从链接使用函数时,它就起作用了。

有人能给我指个方向吗?


解决方案

我忘了回答我自己的问题,但问题来了:

多亏了Blubberdiblb的指导,我已经跟踪了跟踪的开始时间,然后可以添加第一次输入发生时的偏移量。这允许对回放进行精确的计时。

相关文章