懒人小工具

需求:实时监视键盘,当检测到按下'ALT+Z',再按‘BD’就会打开百度网页。或是按下‘GJ’电脑就会关机等(按一次'ALT+Z' 只能执行一次动作)

代码:

#-*- coding: UTF-8 -*-
import os
import pyHook
import win32api
import pythoncom

Lazy_Operation = {
    #网页
    "优快云": 'https://www.youkuaiyun.com/', #优快云
    "BD"  : 'https://www.baidu.com/', #百度
    "FY"  : 'https://translate.google.cn/', #谷歌翻译
    "YJ"  : 'https://outlook.live.com/mail/#/inbox', #hotmail邮箱

    #应用打开
    # "WJSX": r'C:\Users\123\Desktop\e\Everything.exe', #文件快速查找

    #系统控制
    "CLZ" : 'gpedit.msc', #策略组
    "HJBL": 'systempropertiesadvanced', #环境变量

}

error = {
    #系统控制
    "XX"  : 'appwiz.cpl', #卸载应用
    "GJ"  : 'Shutdown -s -t 15', #10秒后关机
    "CQ"  : 'Shutdown -r -t 3', #重启
}

def onKeyboardEvent(event):
    # 监听键盘事件

    file_hot = "D:\hook_logs.txt"
    file_name = "D:\hook_log.txt"

    with open(file_name, 'a+') as forj:

        #键盘数据写入文件
        forj.write(event.Key)
        forj.flush()
        forj.seek(0)
        switch = 'LmenuZ'

        #判断'LmenuZ'是否在文本中
        if switch in forj.read():
            # print 123
            forj.close()

            #快捷键
            key_words = ['FY', 'BD', '优快云', 'YJ', 'GJ', 'WJSX', 'XX', 'CLZ', 'HJBL','QXGJ','CQ']

            #有的话便创建另一个文本,并按下快捷键
            with open(file_hot,'a+') as acc:
                acc.write(event.Key)
                acc.flush()
                for key_word in key_words:
                    try:
                        acc.seek(0)

                        #打开相应的程序或网站
                        if key_word in acc.read():
                            if key_word in Lazy_Operation:
                                win32api.ShellExecute(0, 'open', Lazy_Operation[key_word], '', '', 1)
                                acc.close()
                                #完成后删除文本
                                os.remove(file_name)
                                os.remove(file_hot)

                            elif key_word in error :
                                win32api.ShellExecute(0, 'open', os.system(error[key_word]), '', '', 1)

                    except TypeError:
                        acc.close()
                        os.remove(file_name)
                        os.remove(file_hot)

                    except ValueError:
                        pass
        else:
            # print 456
            pass
    return True

def main():
    # 创建一个:钩子“管理对象
    hm = pyHook.HookManager()
    # 监听所有的键盘事件
    hm.KeyDown = onKeyboardEvent
    #设置键盘”钩子“zz
    hm.HookKeyboard()
    # 进入循环侦听,需要手动进行关闭,否则程序将一直处于监听的状态。可以直接设置而空而使用默认值
    pythoncom.PumpMessages()
    # 我也不知道为什么直接放置到main函数中不管用

if __name__ == "__main__":
    main()

说明:已打包成exe,双击打开就行。如果你想重新设置,添加更多或我们的路径不一样。请下载python2.7版本复制—编辑即可。exe地址:https://download.youkuaiyun.com/download/s990649505/10275633

附打包方法:http://blog.youkuaiyun.com/sunhuaqiang1/article/details/70747545

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值