文章目录
Auto_Copy 是一款专为 Windows 设计的工具,使文本复制和粘贴变得更加便捷。它支持通过鼠标选中文本或使用快捷键 `Ctrl+A` 自动复制文本到剪贴板,并且能够通过右键单击快速粘贴文本内容。
一、灵感来源
- 在使用 Mobaxterm 过程中,我发现其内部具备选中即自动复制和右键粘贴的功能,但限于软件内部使用。
- 由于这一便捷功能,我开发了 Auto_Copy,使其能在整个操作系统中实现类似的功能。
二、主要功能
- 自动复制:鼠标选中或
Ctrl+A
选中文本即自动复制到剪贴板。 - 快速粘贴:右键单击即可快速粘贴剪贴板内容。
三、更新日志
- 2024.06.30:优化代码,显著减小可执行文件大小,从 38MB 优化至 5MB。
- 2024.06.27:完成基本功能开发和测试。
四、Git 链接和截图
五、前置需求和安装
- Python 3.x
- 必需的库:
pyautogui
、clipboard
、pynput
安装步骤:
-
克隆仓库:
git clone https://github.com/sweetorange2022/auto_copy.git cd auto_copy
-
安装所需库:
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
六、使用方法
6.1 使用可执行文件
直接下载 .exe 文件,双击即可运行。
6.2 使用 Python 脚本
6.2.1 运行脚本
```bash
python auto_copy.py
```
6.2.2 如何操作
- 鼠标操作:选中文本即自动复制。
- 快捷键:使用
Ctrl+A
选中文本也自动复制。 - 右键单击:粘贴剪贴板内容到当前光标位置。
6.3 构建可执行文件
使用 PyInstaller 将脚本转换为独立可执行文件:
-
安装 PyInstaller:
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
-
构建可执行文件:
pyinstaller --onefile auto_copy.py
-
查找可执行文件:
完成后,可执行文件将位于
dist
目录中。
七、附代码示例
import time
from pynput import mouse, keyboard
import pyperclip
class AutoCopyTool:
def __init__(self):
self.last_copied_text = ""
self.last_copy_time = 0
self.ctrl_a_pressed = False
self.mouse_listener = mouse.Listener(on_click=self.on_click)
self.keyboard_listener = keyboard.Listener(on_press=self.on_press, on_release=self.on_release)
def start(self):
self.mouse_listener.start()
self.keyboard_listener.start()
self.mouse_listener.join()
self.keyboard_listener.join()
def on_click(self, x, y, button, pressed):
current_time = time.time()
if button == mouse.Button.left and not pressed:
self.copy_to_clipboard()
elif button == mouse.Button.right and not pressed:
self.paste_from_clipboard(current_time)
def on_press(self, key):
try:
if key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r:
self.ctrl_a_pressed = True
except AttributeError:
pass
def on_release(self, key):
if self.ctrl_a_pressed and key == keyboard.KeyCode.from_char('a'):
self.copy_to_clipboard()
if key == keyboard.Key.ctrl_l or key == keyboard.Key.ctrl_r:
self.ctrl_a_pressed = False
def copy_to_clipboard(self):
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 != self.last_copied_text:
self.last_copied_text = current_copied_text
print("复制的内容:", current_copied_text)
self.last_copy_time = time.time()
def paste_from_clipboard(self, current_time):
if current_time - self.last_copy_time < 0.5:
return
current_copied_text = pyperclip.paste()
print("右键复制的内容:", current_copied_text)
keyboard.Controller().press(keyboard.Key.ctrl)
keyboard.Controller().press('v')
keyboard.Controller().release('v')
keyboard.Controller().release(keyboard.Key.ctrl)
self.last_copy_time = current_time
if __name__ == "__main__":
tool = AutoCopyTool()
tool.start()