Write a computer monitoring program in Python

WBOY
Release: 2023-04-11 21:46:33
forward
1778 people have browsed it

Write a computer monitoring program in Python

After the monitoring of fishing activities such as playing games and watching videos, now the tendency of workers to leave the company will also be monitored. Recently some netizens said that their company has installed a behavioral sensing system, which can know in advance employees’ intentions to change jobs.

For a time, there were endless discussions about "it's too difficult to work as a worker" and "no privacy at all".

Some friends asked: Is this kind of monitoring technically feasible? Is it complicated? Today I will show you how to use a few lines of Python code to monitor the operations on your computer.

Monitoring keyboard

If the company secretly runs a background process on our computer to monitor our keyboard events, the simplest python writing method is roughly like this:

from pynput import keyboard

def on_press(key):
    print(f'{key} :pushed')


def on_release(key):
    if key == keyboard.Key.esc:
        return False


with keyboard.Listener(on_press=on_press, on_release=on_release) as lsn:
    lsn.join()


Copy after login

Tap the keyboard at will, and you will see this output from the console:

Write a computer monitoring program in Python

The content of the code is two methods, one is to listen for key events, and the other is Listen for exit events - press ESC and release to exit.

Monitoring mouse

If you still want to monitor mouse events, then just add this code:

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!')


# 定义鼠标监听线程
with mouse.Listener(on_click=on_click) as listener:
    listener.join()


Copy after login

This code is mainly to monitor the left and right click operations of the mouse. Run After operating the mouse, you can see the following results printed on the console:

Write a computer monitoring program in Python

If you are careful, you will definitely find that each click event is printed twice. This is because both pressing and releasing trigger mouse events.

Record monitoring log

Now that there are keyboard events and mouse events, it’s time to combine the two and record user operations in the log. Here we use the loguru module to record logs.

The entire code is as follows:

from pynput import keyboard, mouse
from loguru import logger
from threading import Thread

# 定义日志文件
logger.add('moyu.log')


def on_press(key):
    logger.debug(f'{key} :pushed')


def on_release(key):
    if key == keyboard.Key.esc:
        return False


# 定义键盘监听线程
def press_thread():
    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!')
    else:
        return False


# 定义鼠标监听线程
def click_thread():
    with mouse.Listener(on_click=on_click) as listener:
        listener.join()


if __name__ == '__main__':
    # 起两个线程分别监控键盘和鼠标
    t1 = Thread(target=press_thread())
    t2 = Thread(target=click_thread())
    t1.start()
    t2.start()


Copy after login

After running, you can see the following content in the log file in the same directory:

Write a computer monitoring program in Python

Summary

This article mainly uses the python module ​​pynput​​ to demonstrate how to record keyboard and mouse operations. These simple lines of code are feasible for simple input monitoring, but for complex statements such as chat records, you still need to use NLTK language processing for the logs to restore your chat records.

Of course, we are only discussing the technical feasibility here. Please do not do anything that damages the privacy of others. Besides, with this code alone, others will notice at a glance that there is a program recording operations...

The above is the detailed content of Write a computer monitoring program in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!