AppCreator
AppCreator

Reputation: 241

pyautogui hotkey not working on Mac but on Windows. Why?

The pyautogui hotkey('ctrl', 'shift', '2') is working on Windows but not on Mac (Big Sur, iMac with M1-chip). Can't figure out why that is. (The hotkey should open up a form in a program called PhraseExpress). I have added the Terminal in Privacy->Accessibility. I use the latest Python. I open the py file in the Terminal.

def hotkey() -> None:
    """function to open the corresponding phraseexpress form (with a hotkey)"""
    
if foo == "Note 2":
        if platform == "darwin":
            print("Test before hotkey")
            pyautogui.hotkey('ctrl', 'shift', '2')
        if platform == "win32":
            pyautogui.hotkey('ctrl', 'shift', '2')

        print("You opened PhraseExpress for note 2")

The output is:

Test before hotkey
You opened PhraseExpress for note 2

And nothing else happens. The hotkey is not working on Mac.

It seems like the browser window does not have focus. I need to change focus for it, then the hotkeys will work. How can I change focus to the browser? I am using tkinter and when the script runs it seems like tkinter has the focus, hence the hotkeys will not work

Upvotes: 1

Views: 1609

Answers (1)

Chase Roberts
Chase Roberts

Reputation: 9376

If you are on a mac, most of the functions that you would do with cntrl are done with command. For instance, if you want to do a copy on windows:

pyautogui.hotkey('ctrl', 'c')  # ctrl-c to copy, windows.

But on a mac you need to do:

pyautogui.hotkey('command', 'c')  # command-c to copy, mac.

Upvotes: 3

Related Questions