File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ Python技术 公众号文章代码库
10
10
11
11
## 实例代码
12
12
13
+ [ 用 Python 写最简单的摸鱼监控进程,你会吗?] ( https://github.com/JustDoPython/python-examples/tree/master/xianhuan/monitor ) :用 Python 写最简单的摸鱼监控进程,你会吗?
14
+
13
15
[ 用Python对摩斯密码加解密!] ( https://github.com/JustDoPython/python-examples/tree/master/xianhuan/morse ) : 用Python对摩斯密码加解密!
14
16
15
17
[ Python写春联,瑞雪兆丰年!] ( https://github.com/JustDoPython/python-examples/tree/master/xianhuan/couplets ) : Python写春联,瑞雪兆丰年!
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ @author: 闲欢
5
+ """
6
+ from pynput import keyboard , mouse
7
+ from loguru import logger
8
+ from threading import Thread
9
+
10
+ # 定义日志文件
11
+ logger .add ('moyu.log' )
12
+
13
+
14
+ def on_press (key ):
15
+ logger .debug (f'{ key } :pushed' )
16
+
17
+
18
+ def on_release (key ):
19
+ if key == keyboard .Key .esc :
20
+ return False
21
+
22
+
23
+ # 定义键盘监听线程
24
+ def press_thread ():
25
+ with keyboard .Listener (on_press = on_press , on_release = on_release ) as lsn :
26
+ lsn .join ()
27
+
28
+
29
+ def on_click (x , y , button , pressed ):
30
+ if button == mouse .Button .left :
31
+ logger .debug ('left was pressed!' )
32
+ elif button == mouse .Button .right :
33
+ logger .debug ('right was pressed!' )
34
+ return False
35
+ else :
36
+ logger .debug ('mid was pressed!' )
37
+
38
+
39
+ # 定义鼠标监听线程
40
+ def click_thread ():
41
+ with mouse .Listener (on_click = on_click ) as listener :
42
+ listener .join ()
43
+
44
+
45
+ if __name__ == '__main__' :
46
+ # 起两个线程分别监控键盘和鼠标
47
+ t1 = Thread (target = press_thread ())
48
+ t2 = Thread (target = click_thread ())
49
+ t1 .start ()
50
+ t2 .start ()
You can’t perform that action at this time.
0 commit comments