我使用的程序无法处理空格,所以我制作了一个python脚本来解决我在网上发现的问题,但是存在问题。 当我按Ctrl + c时,第一个字符串被存储,处理并返回到剪贴板并很好地粘贴。但是,当我复制另一个字符串时,将粘贴旧字符串。由于某种原因,剪贴板未使用新字符串进行更新。代码是:
from pynput import keyboard
import pyperclip
import time
# The key combinations to check
COMBINATIONS = [
{keyboard.Key.ctrl_l, keyboard.KeyCode(char='c')},
{keyboard.Key.ctrl_r, keyboard.KeyCode(char='c')}
]
# The currently active modifiers
current = set()
def on_press(key):
if any([key in comb for comb in COMBINATIONS]):
current.add(key)
if any(all(k in current for k in comb) for comb in COMBINATIONS):
main_function()
def on_release(key):
try:
current.remove(key)
except KeyError:
pass
def main_function():
copy = pyperclip.paste()
text = copy.strip()
pyperclip.copy(text)
print("Ctrl + c pressed")#To register if it is pressed
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
编辑:我发现我想同时进行两项操作。复制并格式化字符串。因此,我向该功能添加了时间延迟,并消除了我的问题。
def main_function():
time.sleep(0.1)
copy = pyperclip.paste()
text = copy.strip()
pyperclip.copy(text)
print("Ctrl + c pressed")#To register if it is pressed