剪贴板/pyperclip 的重复输出

duplicate output for clipboard / pyperclip

我想在 txt 中创建一个本地副本 (ctrl+c) 历史记录,但在输出中我不断收到重复的条目。

我的代码:

import keyboard
import win32clipboard

print('Waiting Ctrl+C\n')    

while True

    if keyboard.is_pressed('ctrl+c'):
        win32clipboard.OpenClipboard()
        data = win32clipboard.GetClipboardData()
        a = open('E:\Python\copyhistory.txt', 'a')
        a.write(data)  # text
        a.close()
        win32clipboard.CloseClipboard()
        print('printed')

输出重复了很多。我也试过 pyperclip 但这也没有帮助。 同样的问题也存在于 pyperclip 中。

Pyperclip 示例如下:

import pyperclip
import keyboard

while True:
    if keyboard.is_pressed('ctrl+c'):
        a = pyperclip.paste()
        print(a)

代码 1 的终端输出: 等待 Ctrl+C 打印 然后40行"printed"

pyperclip也一样,代码2用pyperclip输出了700多行相同的输出,应该打印一次的输出。

我哪里做错了,有什么想法吗?

问题是由于 key_down 和 key_up 之间的延迟造成的。这可以通过读取 key_down 或 key_up 来避免。

我试过怎么做,但对我来说是死胡同,所以我决定使用剪贴板中差异的输出。