如何将keyboard.record存储到txt文件并用keyboard.play调用它

问题描述 投票:0回答:1

我正在尝试创建一个可以在 python 中调用的宏,该宏将记录我按下的按钮、按下按钮的时间以及按下按钮之间的时间

我希望能够将宏存储在 txt 文件中,以便稍后可以用另一个函数及时调用它

这是我想到的基本想法,因为我可以使用

rk = keyboard.record(until ='Esc')
,然后在我录制完宏后我使用
keyboard.play(rk, speed_factor=1)
,我刚刚描述的概念效果很好,但我希望能够将
rk = keyboard.record(until ='Esc')
存储到一个 txt 文件,然后能够从该文件中读取并播放它

下面的代码是我试图实现的想法,但它不起作用

from pynput.keyboard import Key, Listener
import keyboard
import time


def recorder():
    rk = keyboard.record(until ='Esc')
    file = open("test.txt", "a")
    for r in rk:
        data = str(r) + "\n"
        file.write(data)

def play_back():
    file = open("test.txt", "r")
    lists = []

    Lines = file.readlines()
    
    count = 0
    # Strips the newline character
    for line in Lines:
        count += 1
        if line[-1] == "\n":
            lists.append(line[:-1])
        else:
            lists.append(line)

    print(lists)

    keyboard.play(lists, speed_factor=1)

这是我调用

play_back()
函数后出现的错误代码

['KeyboardEvent(s down)', 'KeyboardEvent(s up)', 'KeyboardEvent(s down)', 'KeyboardEvent(s up)', 'KeyboardEvent(a down)', 'KeyboardEvent(a up)', 'KeyboardEvent(a down)', 'KeyboardEvent(a up)', 'KeyboardEvent(d down)', 'KeyboardEvent(d up)', 'KeyboardEvent(d down)', 'KeyboardEvent(d up)', 'KeyboardEvent(backspace down)', 'KeyboardEvent(backspace up)', 'KeyboardEvent(backspace down)', 'KeyboardEvent(backspace up)', 'KeyboardEvent(backspace down)', 'KeyboardEvent(backspace up)', 'KeyboardEvent(backspace down)', 'KeyboardEvent(backspace up)', 'KeyboardEvent(backspace down)', 'KeyboardEvent(backspace up)', 'KeyboardEvent(backspace down)', 'KeyboardEvent(backspace up)', 'KeyboardEvent(esc down)', 'KeyboardEvent(left down)', 'KeyboardEvent(left up)', 'KeyboardEvent(down down)', 'KeyboardEvent(down up)', 'KeyboardEvent(right down)', 'KeyboardEvent(right up)', 'KeyboardEvent(right down)', 'KeyboardEvent(right up)', 'KeyboardEvent(s down)', 'KeyboardEvent(s up)', 'KeyboardEvent(s down)', 'KeyboardEvent(s up)', 'KeyboardEvent(d down)', 'KeyboardEvent(d up)', 'KeyboardEvent(a down)', 'KeyboardEvent(a up)', 'KeyboardEvent(s down)', 'KeyboardEvent(s up)', 'KeyboardEvent(esc down)']
Traceback (most recent call last):
  File "c:\Users\pc\Desktop\code\program1\testing122.py", line 33, in <module>
    play_back()
  File "c:\Users\pc\Desktop\code\program1\testing122.py", line 31, in play_back
    keyboard.play(lists, speed_factor=1)
  File "C:\Users\pc\Desktop\code\program1\venv001\Lib\site-packages\keyboard\__init__.py", line 1060, in play
    last_time = event.time
                ^^^^^^^^^^
AttributeError: 'str' object has no attribute 'time'. Did you mean: 'title'?

所以总结一下:我想记录我确切的按钮按下的宏,包括其中的计时(我按下按钮的次数,按下两次按钮之间的时间间隔),然后我希望能够使用以下命令调用该记录python 中的函数,我在这里的方法可能是错误的,所以我会喜欢你的输入

提前致谢

python python-3.x macros
1个回答
0
投票

我已经更新了您的代码,将其序列化为 json 并使用序列化的 json 重新创建对象。不是我通过所做的更改添加的评论。

from pynput.keyboard import Key, Listener
import keyboard
import time
import json


def recorder():
    rk = keyboard.record(until ='Esc')
    file = open("test.txt", "a")

    for r in rk:
        # serialize the object to json format
        data = r.to+json() + "\n"
        file.write(data)

def play_back():
    file = open("test.txt", "r")
    lists = []

    Lines = file.readlines()
    
    count = 0
    # Strips the newline character
    for line in Lines:
        # recreate the object from the json dict by loading the json and
        # use as keyword parms to KeyboardEvent
        lists.append(KeyboardEvent(**json.loads(line)))

    print(lists)

    keyboard.play(lists, speed_factor=1)
© www.soinside.com 2019 - 2024. All rights reserved.