首页 > 解决方案 > Python keyboard.record 不会停止录制

问题描述

我的问题是这段代码:

    replay = k.record(until='esc')     
    print("done recording")
    time.sleep(2);
    print("playing")
    k.play(replay, speed_factor=2)     
    print("done playing")

在我使用keyboard.add_hotkey 调用它之前工作得很好。我尝试使用热键来调用不同的函数,然后调用“my_function”,但这并没有解决它。我猜测热键的激活会冻结任何按键识别。

此外,如果我将它添加到 my_function 中,keyboard.write 仍然有效。目前正在使用解决方法,但解决方法并不能真正解决所有问题。

import pyautogui as p
import time

k.add_abbreviation('@@', 'testing testing testing testing testing testing testing testing')     #this guy allows you to create autocorrect shortcuts


def my_function(): #this is the function that run when you use your hotkey, see below
    while k.is_pressed('alt'): #this makes sure that youre not pressing alt anymore
        pass     #this function makes sure that youre not pressing alt anymore
    #pyautogui.write('Hello There', interval=0.1)     #interval tells python how long to wait between key presses in seconds
    #pyautogui.press('enter')     #more examples
    #pyautogui.write('What is the Weather?', interval=0.1)
    #pyautogui.hotkey('alt', 'f4')     #presses alt and f4 at the same time
    #time.sleep(1)     #tells python to wait 1 second before continuing
    print("recording")
    replay = k.record(until='esc')     #when this code executes, it will record everything you do with your keyboard
    print("done recording")
    time.sleep(2);
    print("playing")
    k.play(replay, speed_factor=2)     #when this code executes, it will replay everything you did with your keyboard. Change speed factor to 2 to replay it in double speed
    print("done playing")
    #p.s, you probably want to run these in different macros.

    #p.hotkey('ctrl','t')
    #p.write('youtube.com')
    #p.press('enter')
    #print("ran")     #writes text into the python window
    print(replay)

k.add_hotkey('alt+a', my_function)     #this is how you tell python to do something when you press alt and 'a' at the same time


print("Press ESC to stop.")
k.wait('esc')     #keyboard waits for you to press escape```

标签: pythonpycharm

解决方案


推荐阅读