1

I have a program in Python that starts another executable. Some automated operations need to be done in the ribbon of this open executable, so I use pyautogui to do so.

First the ribbon needs to be ‘active’, so I click on the left most part. Then I need to use the arrows to change the ribbon menu selection (two times to the left). Then I need to hit enter to open the correct menu. (going from 'File' to 'Scripting')

enter image description here

The code I’m using for this is:

import pyautogui

pyautogui.click(x=0, y=30)
pyautogui.press(['left', 'left']) #this part does not work here
pyautogui.hotkey('enter')

Somehow, the click and enter do work, but the arrow-keys don’t work. I can use the physical arrow-keys to change the menu selection, but this code doesn’t perform these actions somehow.

Does someone know what is wrong here and how to solve this?

Best regards, Ganesh

EDIT:

I tried to open both the program and the script with admin right, but that still didn't work. Somehow, the 'enter' and everything else works, except for the arrows.

Community
  • 1
  • 1
Ganesh Gebhard
  • 453
  • 1
  • 7
  • 20
  • I think you should add sleep between actions. This is happening almost simultaneously – Martinez Oct 25 '19 at 21:49
  • I already tried that (time.sleep(1)), but that also didn't work. – Ganesh Gebhard Oct 25 '19 at 22:13
  • I've had similar problem where it's missing certain keys/mouse buttons on certain windows if your script has no privileges. If your OS is Windows make sure to run the script as Administrator and try again – Martinez Oct 25 '19 at 22:25
  • I tried it just now, but this time the ribbon is activated with the click, but deactivated with the line of pressing the left-arrow button. Thus something happened with that press-function, but not the right thing. – Ganesh Gebhard Oct 25 '19 at 23:23

1 Answers1

1

Ganesh, it might not work with pyautogui as the program/ interface you're using might simply not register the key. To use the arrow keys or other special keys like 'enter' I would suggest using pydirectinput

First, install the library if not already

pip install pydirectinput

Then you can rewrite your code as

import pyautogui
import pydirectinput

pyautogui.click(x=0, y=30)
pydirectinput.press(['left', 'left']) #this is the updated line of code
pyautogui.hotkey('enter')
Dharman
  • 30,962
  • 25
  • 85
  • 135