我正在制作一个非常简单的键盘记录器,每当我键入一个键时,代码都可以工作,但它也会返回一个TypeError: an integer is required (got type NoneType)
。
除此之外,它工作正常。我在网上搜索过,除了pythoncom.PumpMessages()
之外,我发现空白,但pythoncom很烦人,ModuleNotFoundError: No module named 'pywintypes'
。即使我已经下载了 pywin32(并尝试过 pypiwin32(。
这是我的代码:
import pyHook
def keyPress(e):
if e.Ascii:
print(chr(e.Ascii))
if chr(e.Ascii)=="`":
exit()
keylog = pyHook.HookManager()
keylog.KeyDown = keyPress
keylog.HookKeyboard()
一切都很完美,除了每当我按下某个键时都会发生TypeError: an integer is required (got type NoneType)
(除了当我按下'键时,它会退出而不会出错(。
以下是完整的错误消息:
TypeError: an integer is required (got type NoneType)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
** IDLE Internal Exception:
File "C:UsersMaxAppDataLocalProgramsPythonPython37libidlelibrun.py", line 147, in main
handle_tk_events()
File "C:UsersMaxAppDataLocalProgramsPythonPython37libidlelibrun.py", line 80, in handle_tk_events
tcl.eval("update")
SystemError: <built-in method eval of _tkinter.tkapp object at 0x0000024ECF6A9030> returned a result with an error set
[编辑]:
pythoncom 现在可以工作了(虽然我不知道为什么(,但代码仍然抛出错误。
PyHook 要求我在函数中返回一个整数,但我没有返回任何内容,因此在获取 NoneType 时期望一个整数的错误。我只需要添加return 0
.