我正在使用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键,也不会选择文本。我在这里想念的是什么?
修改:
操作系统:Windows 10
环境:Python 3.6.5
答案 0 :(得分:0)
也许尝试一下?
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)
这里有视频证明它可以在我的机器上使用:)
答案 1 :(得分:0)
我刚刚玩弄了pynput和pyautogui,最后发现了使用pynput的组合,可以完成此shift热键功能。我在代码中使用的是page_down而不是箭头键,但我确认两者都可以。由于某些原因,如果同时按下shift键和shift_r键,则可以通过移动光标来突出显示文本。
设置:
from pynput.keyboard import Key, Controller
keyboard = Controller()
代码:
keyboard.press(Key.shift)
keyboard.press(Key.shift_r)
keyboard.press(Key.page_down) # or keyboard.press(Key.left)
keyboard.release(Key.page_down) # or keyboard.release(Key.left)
keyboard.release(Key.shift_r)
keyboard.release(Key.shift)