我写了一个自动发送消息程序
# -*- coding: utf-8 -*-
import time
import threading
from pynput import mouse, keyboard
from tkinter import Tk, Label, Entry, Button
class AutoTyper:
def __init__(self, root):
self.root = root
self.root.title("Auto Typer")
self.root.geometry("300x150")
self.message_label = Label(root, text="Message:")
self.message_label.pack()
self.message_entry = Entry(root)
self.message_entry.pack()
self.interval_label = Label(root, text="Interval (seconds):")
self.interval_label.pack()
self.interval_entry = Entry(root)
self.interval_entry.pack()
self.start_button = Button(root, text="Start", command=self.prepare_typing)
self.start_button.pack()
self.running = False
self.listener = None
self.ready_to_start = False
def prepare_typing(self):
self.message = self.message_entry.get()
self.interval = float(self.interval_entry.get())
if self.listener:
self.listener.stop()
self.listener = mouse.Listener(on_click=self.on_click, on_move=self.on_move)
self.listener.start()
self.ready_to_start = True
def on_click(self, x, y, button, pressed):
if button == mouse.Button.left and pressed:
if self.ready_to_start and not self.running:
self.mouse_position = (x, y)
self.running = True
self.ready_to_start = False
threading.Thread(target=self.type_message).start()
def on_move(self, x, y):
if self.running:
self.running = False
def type_message(self):
my_mouse = mouse.Controller()
my_keyboard = keyboard.Controller()
count = 0
while self.running:
my_keyboard.type(f'{self.message}')
my_keyboard.press(keyboard.Key.enter)
my_keyboard.release(keyboard.Key.enter)
count += 1
time.sleep(self.interval)
if __name__ == "__main__":
root = Tk()
app = AutoTyper(root)
root.mainloop()
增加 ready_to_start 标志:
在 prepare_typing 方法中设置 self.ready_to_start = True 表示准备开始。
在 on_click 方法中,当 self.ready_to_start 为 True 并且 self.running 为 False 时,启动消息发送。
点击鼠标左键时启动消息发送:
仅当 self.ready_to_start 为 True 时记录鼠标位置并开始发送消息,发送消息后将 self.ready_to_start 设置为 False。
鼠标移动时停止消息发送:
保持现有逻辑,当检测到鼠标移动时,将 self.running 设置为 False。