In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Python to write a fish fishing monitoring process", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to use Python to write a fish monitoring process"!
Monitoring keyboard
If the company secretly runs a background process on our computers to monitor our keyboard events, the simplest python goes something 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 output like this from the console:
The content of the code is two methods, one is to listen for the keystroke event, and the other is to listen for the exit event-the ESC button is tapped and released and exited.
Monitoring mouse
If you also want to listen for mouse events, just use this code:
From pynput import mouse def on_click (x, y, button, pressed): if button = = mouse.Button.left: print ('left was presupposed') Elif button = = mouse.Button.right: print ('right was presupposed') Return False else: print ('mid was preset thread') # defines the mouse listening thread with mouse.Listener (on_click=on_click) as listener: listener.join ()
This code mainly monitors the left and right click of the mouse. After running the mouse, you can see the console prints the following results:
If you are careful, you will find that each click event is printed twice. This is because mouse events are triggered by pressing and releasing.
Record monitoring log
There are both keyboard and mouse events, and it's time to combine the two and log the user's actions. Here we use loguru to log, this python module we also talked about in the previous article.
The whole code is as follows:
From pynput import keyboard, mousefrom loguru import loggerfrom 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 presupposed') Elif button = = mouse.Button.right: logger.debug ('right was presupposed') Else: return False # define mouse listening thread def click_thread (): with mouse.Listener (on_click=on_click) as listener: listener.join () if _ _ name__ = ='_ _ main__': # two threads monitor keyboard and mouse T1 = Thread (target=press_thread ()) T2 = Thread (target=click_thread ()) t1.start () t2.start ()
After running it, you can see something like this in the log file in the same directory:
Complete code #! / usr/bin/env python3#-*-coding: utf-8-*-"" @ author: leisure "from pynput import keyboard" Mousefrom loguru import loggerfrom threading import Thread# definition 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# definition 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 presupposed') Elif button = = mouse.Button.right: logger.debug ('right was presupposed') Return False else: logger.debug ('mid was preset thread') # defines mouse listening thread def click_thread (): with mouse.Listener (on_click=on_click) as listener: listener.join () if _ _ name__ = ='_ _ main__': # two threads monitor keyboard and mouse T1 = Thread (target=press_thread ()) T2 = Thread (target=click_thread ()) T1.start () t2.start () so far I believe that "how to use Python to write a fish monitoring process" has a deeper understanding, might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.