【发布时间】:2020-04-21 07:51:56
【问题描述】:
我在下面有以下 python 脚本:
但我想“听”,或者,如果足够的话,只需“记录”到我的 log.txt,以下键:Key.left 和 Key.up。我怎样才能造成这种限制?
这个question 很相似,但她的响应的代码结构有些不同,需要进行重大更改以允许键盘记录器和对Listener 的限制。
在我看来,另一个question 是寻找解决方法的潜在灵感来源。
我花了一些时间寻找一个可以给我这个答案或帮助我反思的问题,但我找不到它,但如果有问题已经发布,请告诉我!
How to create a Python keylogger:
#in pynput, import keyboard Listener method
from pynput.keyboard import Listener
#set log file location
logFile = "/home/diego/log.txt"
def writeLog(key):
'''
This function will be responsible for receiving the key pressed.
via Listener and write to log file
'''
#convert the keystroke to string
keydata = str(key)
#open log file in append mode
with open(logFile, "a") as f:
f.write(keydata)
#open the Keyboard Listener and listen for the on_press event
#when the on_press event occurs call the writeLog function
with Listener(on_press=writeLog) as l:
l.join()
【问题讨论】:
标签: python listener keylogger pynput