游戏专用:星际战甲
APP窗口以及键鼠监控
python3
import tkinter as tk
import time,threading
from pynput.keyboard import Key,Listener
import pynput.keyboard as kb
class myClass:
def __init__(self):
self.root = tk.Tk()
self.new_text = ''
self.flag = threading.Event()
self.th = 0
self.mykey = kb.Controller()
self.delete = kb.Key.delete
def createWin(self):
self.root.title("我的窗口")
self.root.geometry("300x200")
result_text = tk.Text(self.root, height=10, width=50)
result_text.pack()
start_button = tk.Button(self.root, text="case 1", command=self.case1)
start_button.pack()
self.root.mainloop()
def case1(self):
self.winCase1 = tk.Toplevel(self.root)
self.winCase1.title("caseA")
self.new_text = tk.Text(self.winCase1, height=50, width=50)
self.new_text.grid(row=0, column=0,rowspan=50,padx=50, pady=0)
start_button = tk.Button(self.winCase1, text='garuda', command=self.start_garuda)
start_button.grid(row=0, column=0, sticky=tk.W)
start_button = tk.Button(self.winCase1, text='nekros', command=self.start_nekros)
start_button.grid(row=1, column=0, sticky=tk.W)
def wlog(self,msg):
#输出消息到文本框
self.new_text.insert('end', msg+"\n")
self.new_text.yview('end')
def on_release(self,key):
pass
# garuda
def start_garuda(self):
t = threading.Thread(target=self.run_garuda)
t.start()
self.new_text.delete(1.0, tk.END)
msg = "= 开始模拟输入 \n- 停止模拟输入 \ndelete 退出键盘监控"
self.wlog(msg)
def run_garuda(self):
self.run_garuda_ctl()
def garuda_press(self,key):
try:
key = key.char
except AttributeError:
key = key
if key == Key.delete:
self.wlog("停止监听")
self.flag.set()
self.th = 0
return False
elif key == '=':
self.flag.clear() #先清除flag状态,重置为False
if self.th == 0:
self.wlog("开始模拟键盘输入")
t = threading.Thread(target=self.garuda_keyCtr)
t.start()
self.th = 1
elif key == '-':
self.flag.set()
self.th = 0
self.wlog("pause...")
else:
self.wlog("模拟按下了{}".format(key))
def garuda_keyCtr(self):
mykey = self.mykey
while True:
for i in range(0,30):
if self.flag.is_set(): #假如 flag.set()设置了,状态为True
break
mykey.press('1') #按下 1 键
mykey.release('1') #松开 1 键
time.sleep(0.5)
if self.flag.is_set():
break
else:
mykey.press('3')
mykey.release('3')
time.sleep(1)
for j in range(0,2):
if self.flag.is_set():
break
mykey.press('1')
mykey.release('1')
time.sleep(1)
mykey.press('5')
mykey.release('5')
time.sleep(1)
def run_garuda_ctl(self):
self.mykey.press(self.delete)
self.mykey.release(self.delete)
with Listener(on_press=self.garuda_press,on_release=self.on_release) as listener:
listener.join()
# garuda
# nekros
def start_nekros(self):
t = threading.Thread(target=self.run_nekros)
t.start()
self.new_text.delete(1.0, tk.END)
msg = "= 开始模拟输入 \n- 停止模拟输入 \ndelete 退出键盘监控"
self.wlog(msg)
def run_nekros(self):
self.run_nekros_ctl()
def nekros_press(self,key):
try:
key = key.char
except AttributeError:
key = key
if key == Key.delete:
self.wlog("停止监听")
self.flag.set()
self.th = 0
return False
elif key == '=':
self.flag.clear() #先清除flag状态,重置为False
if self.th == 0:
self.wlog("开始模拟键盘输入")
t = threading.Thread(target=self.nekros_keyCtr)
t.start()
self.th = 1
elif key == '-':
self.flag.set()
self.th = 0
self.wlog("pause...")
else:
self.wlog("模拟按下了{}".format(key))
def nekros_keyCtr(self):
mykey = self.mykey
while True:
for i in range(0,10):
if self.flag.is_set(): #假如 flag.set()设置了,状态为True
break
mykey.press('1') #按下 1 键
mykey.release('1') #松开 1 键
time.sleep(4)
if self.flag.is_set():
break
else:
mykey.press('4')
mykey.release('4')
time.sleep(5)
def run_nekros_ctl(self):
self.mykey.press(self.delete)
self.mykey.release(self.delete)
with Listener(on_press=self.nekros_press,on_release=self.on_release) as listener:
listener.join()
if __name__ == "__main__":
mycase = myClass()
mycase.createWin()