Roger
Roger

Reputation: 141

Automate 'enter' key in Python (on a Mac)

I'm creating test automation for an application. I am using a testing tool to do most of the testing, but in order to get to that point I first need to automate one 'enter' key click in Python. I am using a mac, so pywin32 is not available. Any suggestions?

Upvotes: 5

Views: 5438

Answers (4)

ravi kumar
ravi kumar

Reputation: 1

import atomacos

def send_multiple_keys(app,keychr):

    """Send multiple key character(Keyboard Key) with no modifiers."""
     """ app here is the bundle id of the application being used"""

    application=atomacos.getAppRefByBundleId(app)
    application.activate()
    application.sendKeys(keychr)

atomacos can be used for automation on mac OS https://pypi.org/project/atomacos/

Upvotes: 0

Pegasus
Pegasus

Reputation: 1593

    import time
    from pynput.keyboard import Key, Controller   
    keyboard = Controller()
    # Press and release space
    keyboard.press(Key.space)
    keyboard.release(Key.space)

Upvotes: 0

Zach Kelling
Zach Kelling

Reputation: 53829

Appscript makes this pretty easy:

from appscript import app
app('System Events').keystroke('\r')

This will send the keystroke to whichever application is in front.

Upvotes: 1

Jeff Gortmaker
Jeff Gortmaker

Reputation: 4737

By looking around, I found the answer to your question in another question similar to yours.

You're going to have to change up the code a little bit so that it's 'Enter' not Ctrl-r, but it should be easy.

Hope this helps!

Upvotes: 0

Related Questions