ask_me
ask_me

Reputation: 473

Pyautogui hotkey command not working in Pycharm

On Pycharm in Mac, I am using Pyautogui to perform simple operation of Select all with Command+A using hotkey() function.

The code works in scenario:

import pyautogui

pyautogui.press("b")

pyautogui.hotkey("command", "a")

# Cursor here: b

with all text selected. But does not work in the scenario:

import pyautogui

pyautogui.hotkey("command", "a")

# Cursor here: a

where it simply prints the second key in the hotkey which is a

Same thing happens when I do it in Atom. Can someone explain what’s wrong?

Upvotes: 2

Views: 5847

Answers (3)

Arno van Goch
Arno van Goch

Reputation: 128

Note that on MacOS you now need to explicitly grand access to other apps that need to control the keyboard.

You do that by going to Security Preferences > Security & Privacy > Privacy > Accessibility - Give permissions to your terminal application.

Since you were able to control the keyboard somewhat in your case this might not apply to your particular case.

Upvotes: 0

Brifeb
Brifeb

Reputation: 186

In mac os, actually you need to press command key first before any key for the hotkey, so you need to add interval between them.

pyautogui.hotkey("command", "a", interval=0.25 )
pyautogui.hotkey("command", "r", interval=0.25 )  #to refresh page
pyautogui.hotkey("command", "t", interval=0.25 )  #new tab

etc.

Upvotes: 5

okie
okie

Reputation: 881

give it time.sleep(0.1) may work.

If still don't work, try the hard way:

pyautogui.keyDown('command')
pyautogui.keyDown('a')
pyautogui.keyUp('command')
pyautogui.keyUp('a')

Upvotes: 0

Related Questions