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

展开全部

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

这里的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, pyHook

import win32api

import 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 manager

hm = pyHook.HookManager()

# watch for all keyboard events

hm.KeyDown = OnKeyboardEvent

# set the hook

hm.HookKeyboard()

# wait forever

pythoncom.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、付费专栏及课程。

余额充值