Unable to select text using pyautogui

2020-07-25 07:10发布

I want to select some text on the screen. I figured using pyautogui would be the way to go. I did the following:

keyDown('shift')
sleep(0.1)
press('end')
sleep(0.1)
keyUp('shift')

When doing this exact combination by hand it does select the text I want it to. Of course the sleep function is from the time module.

I even tried to select text in word using the following:

keyDown('shift')
press('right')
press('right')
keyUp('shift')

And even this didn't work... Does anyone know why?

Thanks in advance!

3条回答
孤傲高冷的网名
2楼-- · 2020-07-25 07:23

Just found out about 'hotkey' function. Appears to be faster than the single key variation as well.

Example for a copy text and paste per line macro:

import pyautogui as pyg

def macro():

    pyg.press('home')
    pyg.hotkey('shiftleft', 'end')
    pyg.hotkey('ctrl', 'c')
    pyg.press('end')
    pyg.press('enter')
    pyg.hotkey('ctrl', 'v')
    pyg.press('down')

i = macro()

# Do macro 10 times
for i in range(10):
    macro()
查看更多
别忘想泡老子
3楼-- · 2020-07-25 07:25

I found the answer!
It wasn't selecting text for me as long as num lock was enabled.

Make sure num lock is disabled when using pyautogui

查看更多
可以哭但决不认输i
4楼-- · 2020-07-25 07:39

You do not use click twice. Here is an example on how to do it from the documentation:

pyautogui.doubleClick()

PS: I tkink you wanted to do left click...

查看更多
登录 后发表回答