技术工具箱 | 三:Auto_Copy 一个用于Win的自动复制文本的工具

---


Auto_Copy:Windows自动复制文本工具

  Auto_Copy 是一款专为 Windows 设计的工具,使文本复制和粘贴变得更加便捷。它支持通过鼠标选中文本或使用快捷键 `Ctrl+A` 自动复制文本到剪贴板,并且能够通过右键单击快速粘贴文本内容。

一、灵感来源

  • 在使用 Mobaxterm 过程中,我发现其内部具备选中即自动复制和右键粘贴的功能,但限于软件内部使用。
  • 由于这一便捷功能,我开发了 Auto_Copy,使其能在整个操作系统中实现类似的功能。

二、主要功能

  • 自动复制:鼠标选中或 Ctrl+A 选中文本即自动复制到剪贴板。
  • 快速粘贴:右键单击即可快速粘贴剪贴板内容。

三、更新日志

  • 2024.06.30:优化代码,显著减小可执行文件大小,从 38MB 优化至 5MB。
  • 2024.06.27:完成基本功能开发和测试。

四、Git 链接和截图

查看项目源码和下载 Auto_Copy

工具截图

五、前置需求和安装

  • Python 3.x
  • 必需的库:pyautoguiclipboardpynput

安装步骤:

  1. 克隆仓库

    git clone https://github.com/sweetorange2022/auto_copy.git
    cd auto_copy
    
  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
    

六、使用方法

6.1 使用可执行文件

直接下载 .exe 文件,双击即可运行。

6.2 使用 Python 脚本

6.2.1 运行脚本
```bash
python auto_copy.py
```
6.2.2 如何操作
  • 鼠标操作:选中文本即自动复制。
  • 快捷键:使用 Ctrl+A 选中文本也自动复制。
  • 右键单击:粘贴剪贴板内容到当前光标位置。

6.3 构建可执行文件

使用 PyInstaller 将脚本转换为独立可执行文件:

  1. 安装 PyInstaller

    pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
    
  2. 构建可执行文件

    pyinstaller --onefile auto_copy.py
    
  3. 查找可执行文件

    完成后,可执行文件将位于 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()

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
这些都是C++ STL中的算法,用于对容器中的元素进行处理。下面分别给出每个算法的用法和示例: 1. std::remove: 从容器中删除指定的元素 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 2, 4, 2, 5}; auto it = std::remove(vec.begin(), vec.end(), 2); // 删除所有值为2的元素 vec.erase(it, vec.end()); // 删除多余的元素 for (auto x : vec) { std::cout << x << " "; // 输出:1 3 4 5 } return 0; } ``` 2. std::copy_if: 将符合条件的元素复制到新的容器中 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::vector<int> odd_vec; std::copy_if(vec.begin(), vec.end(), std::back_inserter(odd_vec), [](int x) { return x % 2 == 1; }); // 复制所有奇数到odd_vec中 for (auto x : odd_vec) { std::cout << x << " "; // 输出:1 3 5 } return 0; } ``` 3. std::partition: 将容器中的元素按照条件分为两部分,满足条件的在前面,不满足条件的在后面 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; auto it = std::partition(vec.begin(), vec.end(), [](int x) { return x % 2 == 1; }); // 将容器中的元素按照奇偶分为两部分 for (auto x : vec) { std::cout << x << " "; // 输出:1 5 3 4 2 } return 0; } ``` 4. std::unique: 删除容器中相邻的重复元素 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 5, 5}; auto it = std::unique(vec.begin(), vec.end()); // 删除相邻的重复元素 vec.erase(it, vec.end()); // 删除多余的元素 for (auto x : vec) { std::cout << x << " "; // 输出:1 2 3 4 5 } return 0; } ``` 5. std::unique_copy: 将容器中相邻的重复元素复制到新的容器中 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 5, 5}; std::vector<int> unique_vec; std::unique_copy(vec.begin(), vec.end(), std::back_inserter(unique_vec)); // 将相邻的重复元素复制到unique_vec中 for (auto x : unique_vec) { std::cout << x << " "; // 输出:1 2 3 4 5 } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甜橙の学习笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值