所以我想在Python3中创建一个键盘记录器,我已经完成了所有设置。这是我的代码:
from pynput.keyboard import Key, Listener
import logging
ldir = ""
logging.basicConfig(filename=(ldir + "logging.txt"), level=logging.DEBUG, format='%(messages)s:')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
但是这个Traceback异常来了
Traceback (most recent call last):
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\logging\__init__.py", line 992, in emit
msg = self.format(record)
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\logging\__init__.py", line 838, in format
return fmt.format(record)
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\logging\__init__.py", line 578, in format
s = self.formatMessage(record)
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\logging\__init__.py", line 547, in formatMessage
return self._style.format(record)
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\logging\__init__.py", line 391, in format
return self._fmt % record.__dict__
KeyError: 'messages'
Call stack:
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 884, in _bootstrap
self._bootstrap_inner()
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pynput\_util\__init__.py", line 122, in run
self._run()
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pynput\_util\win32.py", line 353, in _run
self._process(msg.wParam, msg.lParam)
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pynput\_util\__init__.py", line 140, in inner
return f(self, *args, **kwargs)
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pynput\keyboard\_win32.py", line 237, in _process
self.on_press(key)
File "C:\Users\ana\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pynput\_util\__init__.py", line 71, in inner
if f(*args) is False:
File "C:\Users\ana\Desktop\logger.py", line 8, in on_press
logging.info(str(key))
Message: "'s'"
Arguments: ()
它没有记录。每按一次按钮就会发生这种情况。任何帮助将受到高度赞赏。谢谢!