gpt4 book ai didi

python 3 : how to use the press Ctrl+X (cut) and Ctrl+V using pynput?

转载 作者:行者123 更新时间:2023-12-03 23:19:19 26 4
gpt4 key购买 nike

关注 pynput documentation我试过这个来“切割”:

1:在编辑器中选择一些文本

2:使用快捷方式运行this_code.py(不离开事件窗口)

from pynput.keyboard import Key, Controller
keyboard = Controller()
with keyboard.pressed(Key.ctrl):
keyboard.press('x')
keyboard.release('x')

python控制台打开实际打印:^X。键的组合是正确的,但它没有做它应该做的:剪切选定的文本将其存储在剪贴板中。 (我对只将剪贴板内容存储在变量中不感兴趣,我想要一个 Ctrl+C)

我猜这个答案也将解决剩下的部分:Ctrl+V(过去一些将首先插入剪贴板的数据)

最佳答案

我考虑了3件事:

  • 因为我使用的是 mac,所以组合是 Command+X 而不是 Ctrl+X
  • 我只能在使用 keyboard.press 时让它工作(按下对我不起作用,不知道为什么)
  • 对于特殊键,我必须使用它们的 Key.value(因此,Key.ctrl 变为 Key.ctrl.value;Key.Shift 变为 Key.Shift.value...)

  • 最后,这对我有用:
    # I tested this code as it is in Mac OS
    from pynput.keyboard import Key, Controller

    keyboard = Controller()

    # keyboard.press(Key.ctrl.value) #this would be for your key combination
    keyboard.press(Key.cmd.value)
    keyboard.press('x')
    keyboard.release('x')
    # keyboard.release(Key.ctrl.value) #this would be for your key combination
    keyboard.release(Key.cmd.value)

    Even though this question is a bit old, I was having this same problem and found a solution that worked for me. Might come in handy for someone in the future.

    关于 python 3 : how to use the press Ctrl+X (cut) and Ctrl+V using pynput?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41743087/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com