# coding: utf-8 import pythoncom#, win32clipboard import pyHook from PIL import ImageGrab import time def getFileName(ext="png"): return time.strftime("%Y%m%d%H%M%S",time.localtime()) + '.' + ext def onKeyboardEvent(event): # 监听键盘事件 """ print "MessageName:", event.MessageName print "Message:", event.Message print "Time:", event.Time print "Window:", event.Window print "WindowName:", event.WindowName print "Ascii:", event.Ascii, chr(event.Ascii) print "Key:", event.Key print "KeyID:", event.KeyID print "ScanCode:", event.ScanCode print "Extended:", event.Extended print "Injected:", event.Injected print "Alt", event.Alt print "Transition", event.Transition print "---" """ #print "KeyID:", event.KeyID #event.Key == "Snapshot": if event.KeyID == 44: """ win32clipboard.OpenClipboard(0) #ll = [win32con.CF_DIBV5,win32con.CF_DIB,win32con.CF_BITMAP] ll = [2,8,17,5,0x0082,0x008E,14,15,16,3,7,9,6,13,12,10,11,4,1] formatid = win32clipboard.GetPriorityClipboardFormat(ll) print formatid s = win32clipboard.GetClipboardData(formatid) print s win32clipboard.CloseClipboard() """ im = ImageGrab.grab() filename = getFileName() im.save(filename,'PNG') print "-" * 10 print filename # 同鼠标事件监听函数的返回值 return True def main(): # 创建一个“钩子”管理对象 hm = pyHook.HookManager() # 监听所有键盘事件 hm.KeyDown = onKeyboardEvent # 设置键盘“钩子” hm.HookKeyboard() # 进入循环,如不手动关闭,程序将一直处于监听状态 pythoncom.PumpMessages() if __name__ == "__main__": main()