我下面有以下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()
您可以从Key
导入pynput.keyboard
模块并检查击键的类型。
#in pynput, import keyboard Listener method
from pynput.keyboard import Listener, Key
#set log file location
logFile = "/home/raj/log.txt"
def writeLog(key):
'''
This function will be responsible for receiving the key pressed.
via Listener and write to log file
'''
if(key == Key.left or key == Key.up):
#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()