Skip to content

Commit c557b46

Browse files
committed
提交代码
1 parent df91c62 commit c557b46

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

xianhuan/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Python技术 公众号文章代码库
1010

1111
## 实例代码
1212

13+
[用 Python 写最简单的摸鱼监控进程,你会吗?](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/monitor):用 Python 写最简单的摸鱼监控进程,你会吗?
14+
1315
[用Python对摩斯密码加解密!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/morse): 用Python对摩斯密码加解密!
1416

1517
[Python写春联,瑞雪兆丰年!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/couplets): Python写春联,瑞雪兆丰年!

xianhuan/monitor/monitor.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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()

0 commit comments

Comments
 (0)