[Tutor] simulating key presses

Alan Gauld alan.gauld at btinternet.com
Tue Mar 8 11:33:15 EST 2016


On 08/03/16 10:00, Benjamin Fishbein wrote:
> Despite scouring stack overflow and other places, I can’t figure out how to simulate key presses.
> I’m using Mac 10.10.5.
> Most answers to this involve sending keys to an element, but my problem is that I have no element to send to; I’m trying to automate key presses for choosing (browsing) a file in the Finder window.
> Any help is much appreciated.

This is specific to your GUI toolkit, in your case Cocoa.
You will need to look in the Apple documentation for Cocoa
on the Apple develop site then see if the required
functionality is made available in the Python Cocoa
bindings available from the MacPython community.

>From my ObjectiveC Cocoa books it looks like you need
to create a Key event using the

+(NSEvent*)keyEventWithType: (NSEventType) type

method.

Another approach would be to use AppleScript via osascript
as a subprocess. You can certainly automate the finder
via that route. It might be easier than going in
via Cocoa. I think there's a Python wrapper for
osascript too.

Not much direct help but maybe a pointer in the
right direction...


HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list