Python监听剪切板实现方法代码实例
作者:凹凸曼大人 时间:2023-11-10 13:48:32
第一种
import win32clipboard
import time
#速度快 容易出错
class niubi():
def lihai(self):
while True:
#jianting().main()
t = jianting().main()
print(t)
class jianting():
def clipboard_get(self):
"""获取剪贴板数据"""
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
win32clipboard.CloseClipboard()
return data
def main(self):
"""后台脚本:每隔0.2秒,读取剪切板文本,检查有无指定字符或字符串,如果有则执行替换"""
# recent_txt 存放最近一次剪切板文本,初始化值只多执行一次paste函数读取和替换
recent_txt = self.clipboard_get()
while True:
# txt 存放当前剪切板文本
txt = self.clipboard_get()
# 剪切板内容和上一次对比如有变动,再进行内容判断,判断后如果发现有指定字符在其中的话,再执行替换
if txt != recent_txt:
# print(f'txt:{txt}')
recent_txt = txt # 没查到要替换的子串,返回None
return recent_txt
# 检测间隔(延迟0.2秒)
time.sleep(0.2)
if __name__ == '__main__':
niubi().lihai()
速度快,但很容易出错, 一般人感觉不出来速度。 不建议使用。
第二种
import pyperclip
import time
#稳定不出错
class niubi():
def lihai(self):
while True:
#jianting().main()
t = jianting().main()
print(t)
class jianting():
def clipboard_get(self):
"""获取剪贴板数据"""
data = pyperclip.paste() #主要这里差别
return data
def main(self):
"""后台脚本:每隔0.2秒,读取剪切板文本,检查有无指定字符或字符串,如果有则执行替换"""
# recent_txt 存放最近一次剪切板文本,初始化值只多执行一次paste函数读取和替换
recent_txt = self.clipboard_get()
while True:
# txt 存放当前剪切板文本
txt = self.clipboard_get()
# 剪切板内容和上一次对比如有变动,再进行内容判断,判断后如果发现有指定字符在其中的话,再执行替换
if txt != recent_txt:
# print(f'txt:{txt}')
recent_txt = txt # 没查到要替换的子串,返回None
return recent_txt
# 检测间隔(延迟0.2秒)
time.sleep(0.2)
if __name__ == '__main__':
niubi().lihai()
我一般把第二种 用在程序中。
来源:https://www.cnblogs.com/aotumandaren/p/13906650.html
标签:Python,监听,剪切板
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Python检测文章抄袭及去重算法原理解析
2023-04-26 12:00:54
![](https://img.aspxhome.com/file/2023/0/85050_0s.jpg)
pymongo实现控制mongodb中数字字段做加法的方法
2021-12-15 21:04:06
Python访问MySQL封装的常用类实例
2024-01-16 01:43:42
浅谈python中的实例方法、类方法和静态方法
2022-02-10 07:11:27
vue3.0封装轮播图组件的步骤
2024-05-09 09:30:16
![](https://img.aspxhome.com/file/2023/9/128869_0s.jpg)
python错误处理详解
2023-04-24 11:10:30
Python包管理工具pip的15 个使用小技巧
2023-08-30 17:30:51
![](https://img.aspxhome.com/file/2023/4/62594_0s.png)
asp.net实现图片以二进制流输出的两种方法
2023-06-28 21:49:02
django-rest-framework 自定义swagger过程详解
2023-01-01 22:05:34
![](https://img.aspxhome.com/file/2023/4/72214_0s.png)
python实现水印图片功能
2021-07-04 00:45:45
![](https://img.aspxhome.com/file/2023/3/96463_0s.jpg)
python工具之清理 Markdown 中没有引用的图片
2022-02-14 11:44:39
![](https://img.aspxhome.com/file/2023/0/67300_0s.png)
SQLSERVER查看数据库日志方法与语句示例代码(已亲测)
2024-01-17 21:32:35
![](https://img.aspxhome.com/file/2023/6/106236_0s.png)
MySql 备忘录
2024-01-22 12:56:48
python中的插入排序的简单用法
2023-09-30 07:57:51
![](https://img.aspxhome.com/file/2023/3/63193_0s.png)
浅谈django不使用restframework自定义接口与使用的区别
2023-11-24 09:44:02
探究Python多进程编程下线程之间变量的共享问题
2023-09-27 15:42:47
Python的Flask框架中web表单的教程
2023-05-17 06:11:06
![](https://img.aspxhome.com/file/2023/4/86634_0s.jpg)
详解MySQL中concat函数的用法(连接字符串)
2024-01-18 09:30:56
Python如何实现自动发送邮件
2022-05-09 04:22:55
![](https://img.aspxhome.com/file/2023/9/90359_0s.png)
Python+Pygame实现之走四棋儿游戏的实现
2023-08-29 21:34:55
![](https://img.aspxhome.com/file/2023/2/62312_0s.png)