当前位置: 首页> 财经> 股票 > 一个用于Win的自动复制文本的工具:Auto_Copy

一个用于Win的自动复制文本的工具:Auto_Copy

时间:2025/8/28 10:32:55来源:https://blog.csdn.net/sweetorange_/article/details/140000009 浏览次数:0次

在这里插入图片描述

自动复制工具



   这是一个用在 Windows 上的的小工具,会将你选中的任何文本保存下来,可以通过点击右键粘贴选中内容。

一、灵感来源:

  • 在使用Mobaxterm时,我注意到其软件中具备选中即自动复制和右键直接粘贴的功能。但是,这种选中自动复制的功能仅在软件内部有效。
  • 由于这一功能极为便捷,我经常在软件外不自觉地尝试进行选中即复制的操作,然后意识到选中自动复制的功能仅在软件内部有效。
  • 因此,我萌生了开发一个工具软件的想法,使整个系统都能实现选中即复制和右键即粘贴的功能。

二、功能

  • 当使用鼠标选中文本时,自动将文本复制到剪贴板。
  • 使用 Ctrl+A 选中文本时,自动将文本复制到剪贴板。
  • 右键单击粘贴剪贴板内容。

三、更新日记

   2024.06.30:更新代码,减少生成的可执行文件的大小 38m - > 5m

   2024.06.27:初步完成全功能

四、Git 链接:auto_copy


在这里插入图片描述

五、前置需求

  • Python 3.x
  • pyautogui
  • clipboard
  • pynput

六、安装

  1. 克隆仓库

    git clone https://github.com/sweetorange2022/auto_copy.git
    cd auto-copy-tool
    
  2. 安装所需库

    pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    

    或单独安装它们:

    pip install pyautogui clipboard pynput -i https://pypi.tuna.tsinghua.edu.cn/simple
    

七、 使用

7.1 软件:
   可以直接使用这个链接里的 .exe : auto_copy, 下载后双击可以直接使用。

7.2 代码使用:

7.2.1. 运行脚本

```bash
python auto_copy.py
```

7.2.2. 如何工作
   - 左键单击 选中文本,文本将自动复制到剪贴板。
   - Ctrl+A 选中文本,文本将自动复制到剪贴板。
   - 右键单击 将剪贴板内容粘贴到光标位置。

** 7.3 构建可执行文件:**

   可以使用 PyInstaller 将此脚本转换为可执行文件。

7.3.1 安装 PyInstaller

```bash
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
```

7.3.2 构建可执行文件

```bash
pyinstaller --onefile auto_copy.py
```

7.3.3 查找可执行文件
可执行文件将位于 dist 目录中。

在这里插入图片描述

八、附代码:

import time
from pynput import mouse, keyboard
import pypercliplast_copied_text = ""
last_copy_time = 0
ctrl_a_pressed = Falsedef on_click(x, y, button, pressed):global last_copied_text, last_copy_timecurrent_time = time.time()# 仅在鼠标左键释放时执行复制操作if button == mouse.Button.left and not pressed:# 发送 Ctrl+C 复制命令keyboard.Controller().press(keyboard.Key.ctrl)keyboard.Controller().press('c')keyboard.Controller().release('c')keyboard.Controller().release(keyboard.Key.ctrl)# 等待剪贴板稳定time.sleep(0.1)# 获取当前剪贴板内容current_copied_text = pyperclip.paste()# 仅在新复制的内容与上次不同的时候更新if current_copied_text != last_copied_text:last_copied_text = current_copied_textprint("复制的内容:", current_copied_text)# 更新上次复制时间last_copy_time = current_time# 右键单击时,复制剪贴板内容并粘贴到光标位置elif button == mouse.Button.right and not pressed:# 控制右键复制操作的频率,至少间隔0.5if current_time - last_copy_time < 0.5:return# 获取当前剪贴板内容current_copied_text = pyperclip.paste()# 打印当前剪贴板内容(用于调试)print("右键复制的内容:", current_copied_text)# 发送 Ctrl+V 粘贴命令keyboard.Controller().press(keyboard.Key.ctrl)keyboard.Controller().press('v')keyboard.Controller().release('v')keyboard.Controller().release(keyboard.Key.ctrl)# 更新上次复制时间last_copy_time = current_timedef on_press(key):global ctrl_a_pressedtry:if key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r:ctrl_a_pressed = Trueexcept AttributeError:passdef on_release(key):global ctrl_a_pressed, last_copied_text, last_copy_timeif ctrl_a_pressed and key == keyboard.KeyCode.from_char('a'):# 发送 Ctrl+C 复制命令keyboard.Controller().press(keyboard.Key.ctrl)keyboard.Controller().press('c')keyboard.Controller().release('c')keyboard.Controller().release(keyboard.Key.ctrl)# 等待剪贴板稳定time.sleep(0.1)# 获取当前剪贴板内容current_copied_text = pyperclip.paste()# 仅在新复制的内容与上次不同的时候更新if current_copied_text != last_copied_text:last_copied_text = current_copied_textprint("Ctrl+A 复制的内容:", current_copied_text)# 更新上次复制时间last_copy_time = time.time()# 重置 ctrl_a_pressedif key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r:ctrl_a_pressed = False# 启动鼠标监听器
mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()# 启动键盘监听器
keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
keyboard_listener.start()# 保持脚本运行
mouse_listener.join()
keyboard_listener.join()

关键字:一个用于Win的自动复制文本的工具:Auto_Copy

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: