我正在尝试编写一个键盘记录器来记录一些用户的输入并通过其他形式自动化他的写作(所以他只需要写一次,并且会有一个文件包含他写的要通过其他形式粘贴的内容)。 我必须使用键盘记录器的原因是由于表单的性质似乎不允许任何其他形式的自动化I / O,而不是鼠标键盘驱动。
所以,在网上看一下我找到了pyHook模块,到目前为止,我已经能够做到这一点:
import pythoncom, pyHook
import sys
f = open('keylogger', 'w+')
def OnKeyboardEvent(event):
if event.Key == 'F12':
f.close()
sys.exit()
if event.Key == 'Space':
f.write(" ")
return True
if event.Key == 'Return':
f.write("\n")
return True
if event.Key == 'Rshift' or event.Key == 'LShift':
return True
if event.Key == 'Oem_4':
f.write("?")
return True
if event.Key == 'Oem_Period':
f.write(".")
return True
if event.Key == 'Back':
a = f.read()
a = a[:-1]
f.write(a)
return True
f.write(event.Key)
return True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
现在我在“OnKeyboardEvent”功能的特定方面遇到问题:
if event.Key == 'Back':
a = f.read()
a = a[:-1]
f.write(a)
return True
我想在这里做的事实是,当用户按“后退”键删除错误时,它应该采取到目前为止在文件中写入的内容并删除最后一个字符。但是,变量a
会产生一个非常奇怪的结果(它是一个非常长的加密字符串,我无法粘贴在这里,但它足以尝试使用键盘记录器一次看看。只是它的开头:{ {1}})。
知道这里发生了什么,我在哪里错了?
附加说明:按键“F12”取消激活键盘记录器,这是用于关闭软件的键,用户将在其中写入要通过其他软件发送的文本。准备好了。