키보드 마우스 후킹 ( Key Mouse Event hooking )
pyHook 이 너무 라이브러리가 잘되어 있다.
MouseEvent , Keyboard Hook 외에도 아래 사이트를 참조하면 라이브러리들에 대한 소개가 있음.
http://www.cs.unc.edu/Research/assist/doc/pyhook/public/pyHook.HookManager-module.html
import os
import time
import pyHook # http://sourceforge.net/projects/pyhook/
from win32gui import GetWindowRect, GetClassName, GetWindowText
##http://sourceforge.net/projects/pywin32/files/pywin32/Build216/
curTime = time.strftime("%Y%m%d_%H%M%S", time.localtime(time.time()))
if not os.path.exists("Messages"):
os.mkdir("Messages")
print "Make Message Directory "
f = open("Messages\\messages"+ curTime +".txt", "w")
def Log(logStr):
print "In logging "
print str(logStr)
f.write(logStr + "\n")
def OnMouseEvent(event):
print "On Mouse Event "
Log('MessageName:' + str(event.MessageName))
Log('Message:' + str(event.Message))
Log('Time:' + str(event.Time))
Log('Window:' + str(event.Window))
if event.Window != 0:
Log('Window Rect:' + str( GetWindowRect(event.Window)))
Log('Window Class Name:' + str( GetClassName(event.Window)))
#Log('Window Text:' + str( GetWindowText(event.Window)))
Log('WindowName:' + str(event.WindowName))
Log('Position:' + str(event.Position))
Log('Wheel:' + str(event.Wheel))
Log('Injected:' + str(event.Injected))
Log('---')
# return True to pass the event to other handlers
# return False to stop the event from propagating
return True
def OnKeyboardEvent(event):
print "On keyboard Event "
Log('MessageName:' + str(event.MessageName))
Log('Message:' + str(event.Message))
Log('Time:' + str(event.Time))
Log('Window:' + str(event.Window))
if event.Window != 0:
Log('Window Rect:' + str( GetWindowRect(event.Window)))
Log('Window Class Name:' + str( GetClassName(event.Window)))
#Log('Window Text:' + str( GetWindowText(event.Window)))
Log('WindowName:' + str(event.WindowName))
Log('Ascii:' + str( event.Ascii) + str( chr(event.Ascii)))
Log('Key:' + str( event.Key))
Log('KeyID:' + str( event.KeyID))
Log('ScanCode:' + str( event.ScanCode))
Log('Extended:' + str( event.Extended))
Log('Injected:' + str( event.Injected))
Log('Alt' + str( event.Alt))
Log('Transition' + str( event.Transition))
Log('---')
# return True to pass the event to other handlers
# return False to stop the event from propagating
return True
# create the hook mananger
hm = pyHook.HookManager()
# register two callbacks
hm.MouseAllButtonsDown = OnMouseEvent
hm.KeyDown = OnKeyboardEvent
# hook into the mouse and keyboard events
hm.HookMouse()
hm.HookKeyboard()
if __name__ == '__main__':
import pythoncom
pythoncom.PumpMessages()
'Python' 카테고리의 다른 글
python iterator 관련itertools 패키지 및 메소드 정리 (0) | 2014.06.01 |
---|---|
python string 관련 메소드 정리 (0) | 2014.06.01 |
[ python ] 정규 표현식으로 문자열 검색하기 (1) | 2014.02.04 |
[python] py2exe 에서 관리자 권한 주기 (0) | 2013.11.06 |
python 프로그램 window 서비스로 등록 시키기 (0) | 2013.10.21 |