Can I use Python to capture keyboard and mouse events in OSX?
Asked Answered
G

5

13

I'm trying to write a simple macro recorder in Python for OSX - something which can capture mouse and key events as the script runs in the background and replay them. I can use autopy for the latter, is there a similarly simple library for the former?

Genitive answered 26/3, 2012 at 1:3 Comment(1)
Some packages mentioned here have OS X support (eg keyboard): https://mcmap.net/q/206429/-key-listeners-in-pythonBond
G
-1

There doesn't seem to be a way of doing this in Python on OSX.

Genitive answered 3/6, 2012 at 6:3 Comment(0)
L
6

I ran across a few solutions to this problem today and figured I'd circle back around and share here so others could save the search time.

A nifty cross platform solution for simulating keyboard and mouse input: http://www.autopy.org/

I also found a brief but working (As of Mountain Lion) example of how to globally log key strokes. The only caveat is that you have to use Python2.6 as 2.7 doesn't seem to have the objc modules available.

#!/usr/bin/python2.6

"""PyObjC keylogger for Python
by  ljos https://github.com/ljos
"""

from Cocoa import *
import time
from Foundation import *
from PyObjCTools import AppHelper

class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler)

def handler(event):
    NSLog(u"%@", event)

def main():
    app = NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    NSApp().setDelegate_(delegate)
    AppHelper.runEventLoop()

if __name__ == '__main__':
   main()

For mouse input, simply replace NSKeyDownMask with the relevant mask from the list available here: http://developer.apple.com/library/mac/#documentation/cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html#//apple_ref/occ/clm/NSEvent/addGlobalMonitorForEventsMatchingMask:handler:

For example, NSMouseMovedMask works for tracking mouse movements. From there, you can access event.locationInWindow() or other attributes.

Lordan answered 4/8, 2012 at 13:19 Comment(0)
B
2

Here's a solution without using curses:

http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time

This question was asked some time back here - Python cross-platform listening for keypresses?

You might find the sample code there helpful!

Bowerman answered 26/3, 2012 at 11:18 Comment(0)
L
1

I know that you can use curses for capturing key input, but im not sure about mouse input. Not only that but if im not mistaken it is included in the std library with 2.7.2.

Lait answered 26/3, 2012 at 11:15 Comment(0)
G
-1

There doesn't seem to be a way of doing this in Python on OSX.

Genitive answered 3/6, 2012 at 6:3 Comment(0)
D
-2

Calvin Cheng, Thank you. your suggestion works on OS X 10.8.5.

Code from http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time

#!/usr/bin/python

import termios, fcntl, sys, os

fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
    while 1:
        try:
            c = sys.stdin.read(1)
            print "Got character", repr(c)
        except IOError: pass
finally:
    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

One more solution Key Listeners in python?

Dunaj answered 5/10, 2013 at 4:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.