0
我試圖用pynput製作一個小鍵盤記錄器。pynput合併感嘆號
這是我的代碼:
from pynput.keyboard import Key, Listener
import os
import logging
log_dir = "C:\WindowsLogs\dist"
filename = "logging"
logging.basicConfig(filename=(log_dir + filename), level=logging.DEBUG, format='%(asctime)s: %(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
一切正常,它也確實節省每一個關鍵。但是,當感嘆號被按下時,它會得到該錯誤:
Traceback (most recent call last):
File "test2.py", line 16, in <module>
listener.join()
File "C:\Python27\lib\site-packages\pynput\_util\__init__.py", line 185, in join
six.reraise(exc_type, exc_value, exc_traceback)
File "C:\Python27\lib\site-packages\pynput\_util\__init__.py", line 140, in inner
return f(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\pynput\keyboard\_win32.py", line 232, in _process
key = self._event_to_key(msg, vk)
File "C:\Python27\lib\site-packages\pynput\keyboard\_win32.py", line 265, in _event_to_key
msg in self._PRESS_MESSAGES))
File "C:\Python27\lib\site-packages\pynput\keyboard\_base.py", line 49, in __init__
'COMBINING ' + unicodedata.name(self.char))
KeyError: "undefined character name 'COMBINING EXCLAMATION MARK'"
代碼有什麼問題?那是什麼意思?
這是您的代碼的一部分,但不運行。製作[mcve]幷包含完整的回溯信息,以便我們知道哪條線路導致了錯誤。 –
向我們展示瞭如何印刷按鍵。我認爲 !是一個特殊的按照pynput –
感謝您的建議。這是我正在運行的代碼。我還添加了整個錯誤消息。我希望你現在可以更好地使用它。 –