A-A+
键盘控制脚本 – 键盘某键连续点击python脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import tkinter as tk
from threading import Thread
import time
import keyboard
class KeyPresser:
def __init__(self, root):
self.root = root
self.root.title("键盘控制工具")
self.start_button = tk.Button(root, text="开始", command=self.start_pressed)
self.start_button.pack(pady=10)
self.stop_button = tk.Button(root, text="停止", command=self.stop_pressed, state=tk.DISABLED)
self.stop_button.pack(pady=10)
self.key_label = tk.Label(root, text="设置需要连续点击的按钮键:")
self.key_label.pack()
self.key_entry = tk.Entry(root)
self.key_entry.pack(pady=10)
self.duration_label = tk.Label(root, text="设置按键间隔(秒):")
self.duration_label.pack()
self.duration_entry = tk.Entry(root)
self.duration_entry.pack(pady=10)
self.count_label = tk.Label(root, text="设定按键执行次数:")
self.count_label.pack()
self.count_entry = tk.Entry(root)
self.count_entry.pack(pady=10)
self.log_text = tk.Text(root, height=10, width=40)
self.log_text.pack(pady=10)
self.running = False
self.total_press_count = 0
self.target_press_count = 0
def start_pressed(self):
start_key = self.key_entry.get()
duration = float(self.duration_entry.get())
self.target_press_count = int(self.count_entry.get())
if not self.running:
self.running = True
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.total_press_count = 0
Thread(target=self.run_key_press, args=(start_key, duration)).start()
def stop_pressed(self):
self.running = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
def run_key_press(self, start_key, duration):
try:
while self.running and self.total_press_count < self.target_press_count:
keyboard.press_and_release(start_key)
self.total_press_count += 1
self.log_text.insert(tk.END, f"按键次数: {self.total_press_count}\n")
self.log_text.see(tk.END) # 滚动到文本框底部
time.sleep(duration)
self.running = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
except keyboard.Unavailable:
self.running = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
key_presser = KeyPresser(root)
root.mainloop() |
运行后截图
通过AI进行了优化,升级版代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import tkinter as tk
from tkinter import filedialog
from threading import Thread
import time
import keyboard
import webbrowser
class KeyPresser:
def __init__(self, root):
self.root = root
self.root.title("键盘控制工具")
self.button_frame = tk.Frame(root)
self.button_frame.pack(pady=10)
self.start_button = tk.Button(self.button_frame, text="开始", command=self.start_pressed)
self.start_button.pack(side=tk.LEFT, padx=5)
self.stop_button = tk.Button(self.button_frame, text="停止", command=self.stop_pressed, state=tk.DISABLED)
self.stop_button.pack(side=tk.LEFT, padx=5)
self.clear_button = tk.Button(root, text="清除日志", command=self.clear_log)
self.clear_button.pack(pady=10)
self.disable_button = tk.Button(root, text="禁用输入", command=self.disable_inputs)
self.disable_button.pack(pady=10)
self.enable_button = tk.Button(root, text="启用输入", command=self.enable_inputs)
self.enable_button.pack(pady=10)
self.save_button = tk.Button(root, text="保存日志", command=self.save_log)
self.save_button.pack(pady=10)
self.key_label = tk.Label(root, text="设置需要连续点击的按钮键:")
self.key_label.pack()
self.key_entry = tk.Entry(root)
self.key_entry.pack(pady=10)
self.duration_label = tk.Label(root, text="设置按键间隔(秒):")
self.duration_label.pack()
self.duration_entry = tk.Entry(root)
self.duration_entry.pack(pady=10)
self.count_label = tk.Label(root, text="设定按键执行次数:")
self.count_label.pack()
self.count_entry = tk.Entry(root)
self.count_entry.pack(pady=10)
self.log_text = tk.Text(root, height=10, width=40)
self.log_text.pack(pady=10)
self.running = False
self.total_press_count = 0
self.target_press_count = 0
def start_pressed(self):
start_key = self.key_entry.get()
duration = float(self.duration_entry.get())
self.target_press_count = int(self.count_entry.get())
if not self.running:
self.running = True
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.total_press_count = 0
Thread(target=self.run_key_press, args=(start_key, duration)).start()
def stop_pressed(self):
self.running = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
def run_key_press(self, start_key, duration):
try:
while self.running and self.total_press_count < self.target_press_count:
keyboard.press_and_release(start_key)
self.total_press_count += 1
self.log_text.insert(tk.END, f"按键次数: {self.total_press_count}\n")
self.log_text.see(tk.END) # 滚动到文本框底部
time.sleep(duration)
self.running = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.log_text.insert(tk.END, "按键执行完成。\n")
self.log_text.see(tk.END) # 滚动到文本框底部
except keyboard.Unavailable:
self.running = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.log_text.insert(tk.END, "按键不可用,请检查按键设置或其他程序占用。\n")
self.log_text.see(tk.END) # 滚动到文本框底部
def clear_log(self):
self.log_text.delete(1.0, tk.END)
def disable_inputs(self):
self.key_entry.config(state=tk.DISABLED)
self.duration_entry.config(state=tk.DISABLED)
self.count_entry.config(state=tk.DISABLED)
def enable_inputs(self):
self.key_entry.config(state=tk.NORMAL)
self.duration_entry.config(state=tk.NORMAL)
self.count_entry.config(state=tk.NORMAL)
def save_log(self):
filename = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
if filename:
with open(filename, "w") as file:
file.write(self.log_text.get(1.0, tk.END))
if __name__ == "__main__":
root = tk.Tk()
key_presser = KeyPresser(root)
root.mainloop() |
软件运行截图如下:


布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏