# -*- coding:utf-8 -*- from ctypes import * import pyHook from pymouse import PyMouse import threading import time import pythoncom import win32api import win32con currenttitle='' lasttitle='' jilu=[] #键盘事件处理,并且发送 m = PyMouse() def OnMouseEvent(event): pass #print 'Position:',event.Position[0] #m.move(event.Position[0]-50, event.Position[1]) def onKeyboardEvent(event): global s global jilu global currenttitle global lasttitle windowTitle = create_string_buffer(512) windll.user32.GetWindowTextA(event.Window,byref(windowTitle),512) currenttitle= windowTitle.value.decode('gbk') c = int(event.Ascii) print c #win 91 #x 88 # 65 #http://blog.csdn.net/Contritio/article/details/77896573 if(c==113): print ("run")
#针对win10,打开功能键 win32api.keybd_event(91, 0, 0, 0) win32api.keybd_event(88, 0, 0, 0) win32api.keybd_event(91, 0, win32con.KEYEVENTF_KEYUP, 0) win32api.keybd_event(88, 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(1) print "wait"
#注销,先U,然后I win32api.keybd_event(85, 0, 0, 0) win32api.keybd_event(85, 0, win32con.KEYEVENTF_KEYUP, 0) win32api.keybd_event(73, 0, 0, 0) win32api.keybd_event(73, 0, win32con.KEYEVENTF_KEYUP, 0)
#功能是实现注销 #win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0) #模拟事件 Conyrol+V,完成复制功能 #win32api.keybd_event(17, 0, 0, 0) # ctrl键位码是17 #win32api.keybd_event(86, 0, 0, 0) # v键位码是86 #win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键 #win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0) if(currenttitle!=lasttitle): jilu=[] jilu.append(currenttitle) jilu.append(c) lasttitle = windowTitle.value.decode('gbk') return True #键盘事件,截获,处理的是上个def函数,onKeyboardEvent def jianpan(): try : hm = pyHook.HookManager() #hm.MouseAll = OnMouseEvent hm.KeyDown = onKeyboardEvent #这里是键盘落下,事件绑定与onKeyboardEvent函数 hm.HookKeyboard() #hm.HookMouse() pythoncom.PumpMessages() except Exception: return threads=[] t2 = threading.Thread(target=jianpan) threads.append(t2) for i in threads: i.start()