python pyhook_python使用pyHook.HookManager()返回来的event中,event.Time肿么转换成为datetime形式?...

我觉得不是时间戳(或者说,不是通常意义下的时间戳)。

这里的event可能是KeyboardEvent或 MouseEvent(视钩子类型而定)。而这两个类又都是HookEvent的子类。

HookEvent有如下几个成员:

Message: Keyboard or mouse event message

Time: Seconds since the epoch when the even current

Window: Window handle of the foreground window at the time of the event

WindowName: Name of the foreground window at the time of the event

这里,对Time成员的描述是:

Seconds since the epoch when the even current

这里的epoch很有意思。

一般上,我们在使用Python中的time模块,或者C标准库中的time.h时,认为epoch是(摘自Python2.7 time模块的文档):

The epoch is the point where the time starts. On January 1st of that year, at 0 hours, the “time since the epoch” is zero. For Unix, the epoch is 1970.

但是,这里的epoch却不是。看下面一段改编自pyhook官网的小例子:

# -*- coding: utf-8 -*-import pythoncom, pyHookimport win32apiimport time def OnKeyboardEvent(event): print event.Time # Return the time in seconds since the epoch as a floating point number. # # The epoch is the point where the time starts. On January 1st of that year, # at 0 hours, the “time since the epoch” is zero. For Unix, the epoch is 1970. print time.time() # Returns the number of milliseconds since windows started print win32api.GetTickCount() print 'MessageName:',event.MessageName print 'Message:',event.Message print 'Time:', time.ctime(time.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 to pass the event to other handlers return True # create a hook managerhm = pyHook.HookManager()# watch for all keyboard eventshm.KeyDown = OnKeyboardEvent# set the hookhm.HookKeyboard()# wait foreverpythoncom.PumpMessages()其中:

print event.Time

print time.time()

print win32api.GetTickCount()

我发现,event.Time和GetTickCount返回的值是一样的。而GetTickCount的含义是:

Returns the number of milliseconds since windows started

也就是,从本次开机到GetTickCount调用时经过的毫秒数。

所以,不能依赖event.Time来获取时间了。而time模块就派上用场了。比如:

print 'Time:', time.ctime(time.time())

就可以打印消息发生时的年月日时分秒了。

取消

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值