Reputation: 39
I found script that by using pyHook could print mouse clicking up and 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 I uncomment this, running the program will freeze stuff, as mentioned earlier.
return True
Record = record()
hm = pyHook.HookManager()
hm.MouseAll = Record.OnMouseEvent
hm.HookMouse()
pythoncom.PumpMessages()
When I used pyHook the same way to detect keys up and down on keyboard it showed me only 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 the user presses a key down anywhere on their system
# the hook manager will call OnKeyboardEvent function.
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
try:
pythoncom.PumpMessages()
except KeyboardInterrupt:
pass
How can I detect key up as well?
Upvotes: 1
Views: 4007
Reputation: 1028
This is really late, but hopefully it helps somebody. You are only registering the hook manager to key down events, so those are the only ones showing. You need to subscribe to KeyUp events as well. You can register them to the same function, shown, but note that probably for a project you'll want to subscribe them to 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 the user presses a key down anywhere on their system
# the hook manager will call OnKeyboardEvent function.
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
# Here we register the same function to the KeyUp event.
# Probably in practice you will create a different function to handle KeyUp functionality
hm.KeyUp = OnKeyboardEvent
hm.HookKeyboard()
try:
pythoncom.PumpMessages()
except KeyboardInterrupt:
pass
Also, depending on your version of Python you may encounter errors if you don't return True at the end of OnKeyboardEvent. You may also want to spend some time reading HookManager.py. Happy keylogging! Er uh, stay safe kids
Upvotes: 3