宏程序不工作

macro program not working

我一直在寻找制作自动点击器的方法,因为我没有通过宏在 Python 中使用 clicking/typing 的任何经验。我希望程序能够检测到我何时按下按钮 (F1) 并开始不断点击,直到我按下停止按钮 (F2);不幸的是,我的代码不会输出 cps 变量以及 xy 变量。我只需要能够检测到它在那里工作即可继续我的实际点击。

基本上,我是在问如何修复按键检测。 Python版本:3.6.5

编辑:我知道它会检查 1 和 2,当按下 f1 时会打开一个 python 帮助屏幕 - 所以现在我只是在做 1 和 2

import random, pygame, pyautogui, time 
loop = 1
on = 0
pygame.init()
while(loop == 1):
    key = pygame.key.get_pressed()
    if(key[pygame.K_1]):
        on = 1
    elif(key [pygame.K_2]):
        on = 0
    if(on == 1):
        x,y = pygame.mouse.get_pos()
        cps = random.randint(10,20)
        print(cps, x,y)

您的代码目前正在检查 12 数字键。

您需要 K_F1K_F2,而不是 K_1K_2 作为功能键。

定义一个用户事件并调用 pygame.time.set_timer 并将此事件作为第一个参数,pygame 将在指定的时间间隔后开始将事件添加到队列中。

import random
import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
CLICK_EVENT = pg.USEREVENT + 1

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_1:
                pg.time.set_timer(CLICK_EVENT, 1000)  # Start the timer.
            elif event.key == pg.K_2:
                pg.time.set_timer(CLICK_EVENT, 0)  # Stop the timer.
        elif event.type == CLICK_EVENT:
            print(random.randint(10, 20), pg.mouse.get_pos())

    screen.fill(BG_COLOR)
    pg.display.flip()
    clock.tick(30)

pg.quit()