python - Using pyHook to detect key up and down -
i found script using pyhook print mouse clicking , down:
class record(object): def onmouseevent(self, event): print 'messagename:',event.messagename print 'message:',event.message print 'time:',event.time print 'window:',event.window print 'windowname:',event.windowname print 'position:',event.position print 'wheel:',event.wheel print 'injected:',event.injected print '---' #time.sleep(1) #if uncomment this, running program freeze stuff, mentioned earlier. return true record = record() hm = pyhook.hookmanager() hm.mouseall = record.onmouseevent hm.hookmouse() pythoncom.pumpmessages()
when used pyhook same way detect keys , down on keyboard showed me key down
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 ('---') return true # when user presses key down anywhere on system # hook manager call onkeyboardevent function. hm = pyhook.hookmanager() hm.keydown = onkeyboardevent hm.hookkeyboard() try: pythoncom.pumpmessages() except keyboardinterrupt: pass
how can detect key well?
this late, helps somebody. registering hook manager key down events, ones showing. need subscribe keyup events well. can register them same function, shown, note project you'll want subscribe them different methods.
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 ('---') return true # when user presses key down anywhere on system # hook manager call onkeyboardevent function. hm = pyhook.hookmanager() hm.keydown = onkeyboardevent # here register same function keyup event. # in practice create different function handle keyup functionality hm.keyup = onkeyboardevent hm.hookkeyboard() try: pythoncom.pumpmessages() except keyboardinterrupt: pass
also, depending on version of python may encounter errors if don't return true @ end of onkeyboardevent. may want spend time reading hookmanager.py. happy keylogging! er uh, stay safe kids
Comments
Post a Comment