我想在python中创建一个简单的键盘记录器而不需要任何外部模块,即使它没有焦点也会捕获每个按键。我尝试过使用此代码:
from tkinter import *
from time import clock, sleep
recorder = Tk()
recorder.config(background="black")
recorder.geometry("1x1+0+0")
recorder.wm_attributes("-alpha",0.01)
recorder.wm_attributes("-topmost",True)
recorder.overrideredirect(True)
def key(event):
if event.char == event.keysym:
msg = 'Normal Key %r' % event.char
elif len(event.char) == 1:
msg = 'Punctuation Key %r (%r)' % (event.keysym, event.char)
else:
msg = 'Special Key %r' % event.keysym
print(msg)
recorder.focus_displayof()
recorder.bind_all('<Key>', key)
recorder.mainloop()
但是当它失去焦点时它会停止反应,有人可以帮助正确使用recorder.focus_displayof()吗?