【问题标题】:Listening to specific keys with pynput.Listener and keylogger?使用 pynput.Listener 和键盘记录器监听特定键?
【发布时间】: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


    【解决方案1】:

    您可以从pynput.keyboard 导入Key 模块并检查击键的类型。

    #in pynput, import keyboard Listener method
    from pynput.keyboard import Listener, Key
    
    #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
        '''
    
        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()
    

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 2021-02-11
      • 2019-12-07
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      相关资源
      最近更新 更多