Use Python to monitor what my son does on the computer every day

Following the monitoring of fishing behaviors such as playing games and watching videos, the turnover intention of migrant workers will also be monitored.

Some netizens broke the news that Zhihu was laying off low-key staff, and the video related departments had to lay off almost half. In the discussion area of Zhihu layoffs, some netizens said that the enterprise has installed a behavior perception system, which can know the employees' idea of job hopping in advance.

While denying the layoff plan, Zhihu also stated that it had never installed and used the online behavior perception system, and would not enable similar software tools in the future.

Because of this matter, I am deeply convinced that it has been pushed to the forefront, and the public opinion has paid more and more attention.

For a time, discussions about "it's too difficult for workers" and "there's no privacy" emerged one after another.

Today, I'll show you how to write a few lines of Python code to monitor the computer.

Monitoring keyboard

If the company secretly runs a background process on our computer to monitor our keyboard events, the simplest way to write python is roughly as follows:

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()

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

The content of the code is two methods: one is to listen to key events, and the other is to listen to exit events - click the "ESC" key and release it to exit.

Dry goods mainly include:

① More than 200 Python e-books (and classic books) should have

② Python standard library materials (the most complete Chinese version)

③ Project source code (forty or fifty interesting and reliable hand training projects and source code)

④ Videos on basic introduction to Python, crawler, network development and big data analysis (suitable for Xiaobai learning)

⑤ Python learning roadmap (bid farewell to non mainstream learning)
 

Python exchange of learning Q Group 101677771

Monitor mouse

If you want to listen for mouse events, you can use 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!')


# Define mouse listening thread
with mouse.Listener(on_click=on_click) as listener:
    listener.join()

This code is mainly used to monitor the click operation of the left and right buttons of the mouse. After running the mouse, you can see the following results printed on the console:

Careful you will find that each click event is printed twice. This is because pressing and releasing both trigger mouse events.

Record monitoring log

With both keyboard events and mouse events, it's time to combine the two and log the user's actions. Here we use loguru to record logs. We also talked about this python module in our previous articles.

The whole code is as follows:

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

# Define log file
logger.add('moyu.log')


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


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


# Define keyboard listening thread
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


# Define mouse listening thread
def click_thread():
    with mouse.Listener(on_click=on_click) as listener:
        listener.join()


if __name__ == '__main__':
    # Start two threads to monitor the keyboard and mouse respectively
    t1 = Thread(target=press_thread())
    t2 = Thread(target=click_thread())
    t1.start()
    t2.start()

After running, you can see the following contents in the log file under the same level directory:

This article mainly explains how to record the operation of keyboard and mouse through the python module "pynput". These simple lines of code can be used for simple operations such as monitoring and entering passwords, but for complex statements such as chat records, you also need to use NLTK language for log processing in order to recover your chat records.

After learning this, what do you want to do most? Welcome to comment area!

Keywords: Python Back-end

Added by Strings on Mon, 21 Feb 2022 03:50:11 +0200