直接上代码:
import pyHook #下载地址 http://sourceforge.net/projects/pyhook/files/pyhook/ import pythoncom #下载地址 http://sourceforge.net/projects/pywin32/ ##处理键盘事件 def onKeyboardEvent(event): print 'Window Name:' + event.WindowName print 'Event Key:' + event.Key return True #创建Hook句柄 hm = pyHook.HookManager() # 监控键盘 hm.KeyDown = onKeyboardEvent hm.HookKeyboard() #循环获取消息 pythoncom.PumpMessages()