栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python使用keyboard库写的GUI键盘宏

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python使用keyboard库写的GUI键盘宏

前言:之前和朋友玩游戏,需要一直按住两个按键,很麻烦。就像用python写个小脚本来方便自己,说干就干。(用于学习)

正文:
用到的库:

  1. keyborad
  2. threading
  3. tkinter
  4. time

分析:由于需要监听键盘与运行可视化界面,所以要用的python的多线程模块:threading。

线程:

  1. 监听键盘,是否运行/停止键盘宏。
  2. GUI界面线程。
  3. 键盘宏运行线程。

一,编写:
利用tkinter模块构造前端:

class GUI(threading.Thread):   #从threading.Thread中继承一个新的子类
    def __init__(self):
        threading.Thread.__init__(self)
        global text_list
        text_list = []
    def run(self):
        global d,f,h23
        windows = tk.Tk()
        windows.title = '键盘精灵'
        windows.geometry('500x500')
        a = tk.Label(windows, text='请按照你需要键盘顺序,按下键盘按键:n例如:你需要让键盘循环输入:”Q“+"W"+"E"+"R"n在下面右边的输入框输入:qwern开始热键为e,停止热键为t', bg='white',
                     font=('Arial', 12), width=40, heigh=5)
        a.pack(side='top', expand='no', fill='x')
        c1 = tk.Button(windows, text='任意位置插入', bg='white', font=('Arial', 12), width=20, heigh=1, command=self.insert)
        c2 = tk.Button(windows, text='末尾位置插入', bg='white', font=('Arial', 12), width=20, heigh=1, command=self.end)
        c3 = tk.Button(windows, text='清空', bg='white', font=('Arial', 12), width=20, heigh=1, command=self.clear)
        c4 = tk.Button(windows, text='确定', bg='white', font=('Arial', 12), width=20, heigh=1, command=self.ok)
        f = tk.Text(width='40', heigh='1')
        f.pack(side='left', expand='yes', fill='both', anchor='w')
        d = tk.Entry(windows, show=None, width=50, font=('Arial', 30))
        d.pack(side='top', expand='yes', fill='both', anchor='n')
        c1.pack(side='top', expand='yes', fill='both')
        c2.pack(side='top', expand='yes', fill='both')
        c3.pack(side='top', expand='yes', fill='both')
        c4.pack(side='top', expand='yes', fill='both')
        windows.mainloop()

    def insert(self):
        global d, f
        var = d.get()
        f.insert('insert', var)

    def end(self):
        global d, f
        var = d.get()
        f.insert('end', var)

    def clear(self):
        global d, f
        f.delete(0.0, 'end')

    def ok(self):
        global d, f
        h = f.get(0.0, 'end')
        text_list.extend(h)

然后就是编写后台的程序了:

#下面这个类是用于储存从前端获得的数据,并处理,方便下面程序利用
class judge(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        global list
        list = []
        pass
    def run(self):
        while True:
            global text_list
            a = text_list
            if len(a) > 0:
                if a != list:
                    list.clear()
                    list.extend(a)
                elif a==list:
                    text_list.clear()
            elif len(a) == 0:
                time.sleep(1)
                pass
#这个类是用于监听键盘的‘e’键,看是否开始运行键盘宏;
class LISTEN(threading.Thread):  
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        while True:
            keyboard.wait('e')
            LISTEN.QW(self)
    #下面这个函数,用于循环发送特定的按键,实现键盘宏;
    def QW(self):
        global n
        while True:
            for i in list:
                keyboard.send(i)
                time.sleep(0.1)
                # keyboard.send('w')
                # time.sleep(0.1)
                if n == 1:
                    break
            if n == 1:
                n = 0
                break
#下面这个类是用来监听键盘,用户是否按下“t”键来退出键盘循环;
class LISTEN_EXIT(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        global n
        while True:
            keyboard.wait('t')
            n = 1

最后就是将各个类,实例化运行:

if __name__ == '__main__':
    gui = GUI()
    JU = judge()
    listen = LISTEN()
    listen_exit = LISTEN_EXIT()
    gui.start()
    JU.start()
    listen.start()
    listen_exit.start()
    gui.join()
    JU.join()
    listen.join()
    listen_exit.join()

程序写的不是很好,有很多地方啰嗦或重复的地方,之后会不断完善。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/280671.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号