在 mac 上使用 Python 将剪贴板历史记录存储在文件中

Store clipboard history in file using Python on mac

我需要从文本中复制一些单词,我想将复制的单词存储在一个文件中。基本上是一个包含剪贴板历史记录的文件。

由于单词的复制是一个连续的过程,我需要使用一个循环来存储表演期间复制的单词。我认为使用 action/event 侦听器会很方便 1) 在按下 cmd + x 时将剪贴板数据存储到文件中,以及 2) 在按下 cmd + q 时结束循环。

我对 action/event 听众不是很熟悉,也不确定我应该如何使用它们。我可以使用任何简单的实现吗?

我有一个主要是伪代码,它描述了我想要完成的事情。任何建议表示赞赏!

import pyperclip
import os
import os.path
import time

save_path = "/Users/username/file"
FileName = os.path.join(save_path, "clipboard.txt")         
f= open(FileName, "w")    

text = pyperclip.paste()  # text will have the content of clipboard

x = 0
while True:
    time.sleep(2) #Replace with-->: wait for "cmd + x" to be pressed
    text = pyperclip.paste()  # text will have the content of clipboard
    f.write(text+"\n")
    x += 1
    if x > 10: #Replace with --> break if "cmd + q" is pressed 
        break

f.close()
print("End of session")

上面的代码在将文本从剪贴板复制到文件之前等待 2 秒。循环在 10 次迭代后到达终点。

我希望代码对执行的操作做出反应,以便我可以将所有复制的单词存储到一个文件中。

编辑:使用问题回复中的建议,我尝试通过两种方式解决问题,一种是使用键盘和 pynput 模块。键盘对我不起作用,可能是因为我使用了 Mac?按键时没有按预期反应。

键盘代码#(不起作用)

while True:
    try:
        if keyboard.is_pressed('c'):
            print('Copying the selected')
            text = pyperclip.paste()  # text will have the content of clipboard
            f.write(text+"\n")
            sleep(0.1)  # Avoiding long press, remove to see effect
            # Your Code
        elif keyboard.is_pressed('esc'):
            print('Exiting Code')
            break
    except Exception as error:
        print(error)
f.close()

问题:当按下 'c' 键时,没有任何反应。打印语句不激活。

pynput 代码 #(有效)

def on_release(key):
    if str(key) == "'c'":
        print('Copying the slected')
        text = pyperclip.paste()  # text will have the content of clipboard
        f.write(text+"\n")

    print('{0} release'.format(
        key))
    if key == Key.esc:
        # Stop listener
        f.close()
        print("End")
        return False


# Collect events until released
with Listener(
        on_release=on_release) as listener:
    listener.join()

pynput 按预期工作,每次按下 'c' 键时,就像使用 cmd + c 复制时所做的那样,它将剪贴板中当前存储的文本复制到文本文件。

(次要)问题:我没有让 pynput 对同时按下 cmd + c 的组合做出反应。然而,在这种情况下这并不重要,因为对按键 'c' 的反应就足够了。不过,为了将来的参考,如果您知道如何收听组合键,请发表评论。

我尝试了以下方法,但均无效:

if key == Key.cmd and str(key) == "'c'":
        print('Copying the selected')

if key == Key.cmd:
    if str(key) == "'c'":
        print('Copying the selected')

在这种情况下,您应该使用 python 的键盘库 https://pypi.org/project/keyboard/

代码的基本结构如下所示

import keyboard
from time import sleep

while True:
    try:
        if keyboard.is_pressed('ctrl+c'):
            print('Copying the slected')
            sleep(0.1)  # Avoiding long press, remove to see effect
            # Your Code

        elif keyboard.is_pressed('esc'):
            print('Exiting Code')
            break

    except Exception as error:
        print(error)

你可以试试这个用pynput,一个函数支持多组合键映射任意自定义函数,把它们写成一个dict:

def multi_hotkey_listen(hotkey_dict):
    """Multi-hotkey listen"""

    # Create a mapping of hot keys to function, use hashable frozenset as keys
    # Original set type is not hashable - so they can't be used as keys of dict
    frozen_hotkey_dict = {frozenset(k.split('+')): v for k, v in hotkey_dict.items()}
    # Currently pressed keys
    current_keys = set()

    def on_press(key):
        """When a key is pressed, add it to the set we are keeping track of
        and check if this set is in the dictionary"""
        try:
            key_str = key.char
        except AttributeError:
            key_str = key.name

        current_keys.add(key_str)
        # print(current_keys)

        if frozenset(current_keys) in frozen_hotkey_dict:
            # If the current set of keys are in the mapping, execute the function
            trigger_func = frozen_hotkey_dict[frozenset(current_keys)]
            print(f'Success:function {trigger_func.__name__} be triggered', )
            trigger_func()

    def on_release(key):
        """When a key is released, remove it from the set of keys we are keeping track of"""
        try:
            key_str = key.char
        except AttributeError:
            key_str = key.name
        current_keys.remove(key_str)

    with KeyboardListener(on_press=on_press, on_release=on_release, suppress=False) as listener:
        listener.join()


hotkey_dict = {
    'cmd+x': save_data  # support combination key like ctrl+shift+1
    'cmd+q': end_loop,
}

multi_hotkey_listen(hotkey_dict)