-
Notifications
You must be signed in to change notification settings - Fork 1
/
clipboard_listener.py
36 lines (32 loc) · 1.41 KB
/
clipboard_listener.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pyperclip
import time
class ClipboardListener:
@staticmethod
def handleClipboardChangeAndDoSomething(callBackFunction):
"""
检测剪贴板变动,如果复制了文本,则处理文本后,再重新设置剪贴板
:param callBackFunction: 剪贴板变更后调用的函数
:return:
"""
prev = pyperclip.paste()
print(prev)
while True:
time.sleep(0.1)
# 把新的剪贴板文本拿到,和旧的做对比,如果不一样则进行处理
# 当剪贴板为非文本的时候,获取的是空字符串
try:
nowText = pyperclip.paste()
if (nowText != prev) and (nowText != ''):
print("Clipboard has changed!, from:", prev, "--to-->", nowText)
textAfterChange = callBackFunction(nowText)
# 必须在重新设置剪贴板之前,把处理后的文本赋值到nowText表示当前剪贴板内容
nowText = textAfterChange
prev = nowText
ClipboardListener.addToClipBoard(textAfterChange)
print("clipboard content now:\n", nowText)
except pyperclip.PyperclipWindowsException as e:
print("Error:", e)
@staticmethod
def addToClipBoard(text):
"添加文本到剪贴板"
pyperclip.copy(text)