gpt4 book ai didi

python - 无法在 pynput 中选择文本

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:45 25 4
gpt4 key购买 nike

我正在使用 pynput 库编写一个 python 脚本,我想在其中选择特定长度的文本。

为此,我首先按 Shift 键,然后按多次向左箭头键。这样光标 id 左边的文本就被选中并可以进一步用于复制目的。

这是我的代码:

from pynput import keyboard
from pynput.keyboard import Controller

keyboard_controller = Controller()
with keyboard_controller.pressed(keyboard.Key.shift_l):
keyboard_controller.press(keyboard.Key.left)
keyboard_controller.release(keyboard.Key.left)
keyboard_controller.press(keyboard.Key.left)
keyboard_controller.release(keyboard.Key.left)
keyboard_controller.press(keyboard.Key.left)
keyboard_controller.release(keyboard.Key.left)

这会将光标向左移动 3 步,但即使按下 Shift 键,文本也不会被选中。我在这里缺少什么?

编辑:

OS: Windows 10

Environment: Python 3.6.5

这是我的 vs 代码中发生的情况的演示。 enter image description here

最佳答案

也许试试这个?

from pynput.keyboard import Key
from pynput.keyboard import Controller

keyboard = Controller()

选项 1:

with keyboard.pressed(Key.shift):
keyboard.press(Key.left)
keyboard.release(Key.left)
keyboard.press(Key.left)
keyboard.release(Key.left)

选项 2:

keyboard.press(Key.shift)
keyboard.press(Key.left)
keyboard.release(Key.left)
keyboard.press(Key.left)
keyboard.release(Key.left)
keyboard.release(Key.shift)

选项 3:

keyboard.press(Key.shift.value)
keyboard.press(Key.left.value)
keyboard.release(Key.left.value)
keyboard.press(Key.left.value)
keyboard.release(Key.left.value)
keyboard.release(Key.shift.value)

这是它在我的机器上运行的视频证明:)

enter image description here

关于python - 无法在 pynput 中选择文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52799836/

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