我发现脚本使用pyHook可以打印鼠标上下点击:
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()
当我用pyHook以相同的方式在键盘上上下检测键时,它显示我只按下键
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
我如何检测密钥?
答案 0 :(得分:2)
这真的很晚了,但希望它对某人有所帮助。您只是注册了关键事件的钩子管理器,因此这些是唯一显示的事件。您还需要订阅KeyUp事件。您可以将它们注册到显示的相同功能,但请注意,您可能希望将它们订阅到不同的方法。
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
此外,根据您的Python版本,如果在OnKeyboardEvent结尾处未返回True,则可能会遇到错误。您可能还想花一些时间阅读HookManager.py。快乐的键盘记录!呃,保持安全的孩子