【发布时间】:2020-08-24 01:19:24
【问题描述】:
我在 win7 64 位上使用 python 2.7。这是一个代码,它只是将每个选定的text 替换为<h1>text</h1>。 pynput-1.6.8 用于全局热键和按键,pyperclip-1.7.0 用于处理剪贴板。
但我发现其实CTRL+C根本没有按下。
有什么问题?谢谢
from pynput.keyboard import Key, Controller, GlobalHotKeys
import pyperclip
# initilize the clipboard to null
pyperclip.copy('')
keyboard = Controller()
def on_activate_h():
print('<ctrl>+<alt>+h pressed')
# copy current text to clipboard
# but in fact, it does not on my PC
# why
keyboard.press(Key.ctrl)
keyboard.press('c')
keyboard.release('c')
keyboard.release(Key.ctrl)
txt = pyperclip.paste()
if txt:
keyboard.type(f'<h1>{txt}</h1>')
def on_activate_i():
print('<ctrl>+<alt>+i pressed')
with GlobalHotKeys({
'<ctrl>+<alt>+h': on_activate_h,
'<ctrl>+<alt>+i': on_activate_i}) as h:
h.join()
【问题讨论】:
标签: python automation keyboard clipboard