游戏专用....

游戏专用:星际战甲

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()
相关推荐
封步宇AIGC18 分钟前
量化交易系统开发-实时行情自动化交易-3.4.1.2.A股交易数据
人工智能·python·机器学习·数据挖掘
何曾参静谧18 分钟前
「Py」Python基础篇 之 Python都可以做哪些自动化?
开发语言·python·自动化
Prejudices22 分钟前
C++如何调用Python脚本
开发语言·c++·python
单音GG25 分钟前
推荐一个基于协程的C++(lua)游戏服务器
服务器·c++·游戏·lua
我狠狠地刷刷刷刷刷35 分钟前
中文分词模拟器
开发语言·python·算法
Jam-Young1 小时前
Python的装饰器
开发语言·python
Mr.咕咕1 小时前
Django 搭建数据管理web——商品管理
前端·python·django
AnFany1 小时前
LeetCode【0028】找出字符串中第一个匹配项的下标
python·算法·leetcode·字符串·kmp·字符串匹配
爪哇学长2 小时前
Java API类与接口:日期类型与集合的使用
java·开发语言·python
封步宇AIGC2 小时前
量化交易系统开发-实时行情自动化交易-3.4.1.6.A股宏观经济数据
人工智能·python·机器学习·数据挖掘