【问题标题】:Can I use Python to capture keyboard and mouse events in OSX?我可以使用 Python 在 OSX 中捕获键盘和鼠标事件吗?
【发布时间】:2012-03-26 01:03:39
【问题描述】:

我正在尝试用 Python 为 OSX 编写一个简单的宏记录器——它可以在脚本在后台运行时捕获鼠标和按键事件并重放它们。后者我可以使用autopy,前者有没有类似的简单库?

【问题讨论】:

标签: python macos keyboard-hook


【解决方案1】:

我今天遇到了这个问题的一些解决方案,并想我会转回来在这里分享,以便其他人可以节省搜索时间。

用于模拟键盘和鼠标输入的漂亮跨平台解决方案:http://www.autopy.org/

我还找到了一个简短但有效的(截至 Mountain Lion)示例,说明如何全局记录击键。唯一需要注意的是,您必须使用 Python2.6,因为 2.7 似乎没有可用的 objc 模块。

#!/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()

对于鼠标输入,只需将NSKeyDownMask 替换为此处可用列表中的相关掩码:http://developer.apple.com/library/mac/#documentation/cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html#//apple_ref/occ/clm/NSEvent/addGlobalMonitorForEventsMatchingMask:handler

例如,NSMouseMovedMask 用于跟踪鼠标移动。 从那里,您可以访问 event.locationInWindow() 或其他属性。

【讨论】:

    【解决方案2】:

    这是一个不使用curses的解决方案:

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

    这个问题曾经在这里问过一段时间 - Python cross-platform listening for keypresses?

    您可能会发现那里的示例代码很有帮助!

    【讨论】:

      【解决方案3】:

      我知道您可以使用curses 来捕获键输入,但我不确定鼠标输入。不仅如此,如果我没记错的话,它还包含在 2.7.2 的 std 库中。

      【讨论】:

        【解决方案4】:

        在 OSX 上的 Python 中似乎没有办法做到这一点。

        【讨论】:

          【解决方案5】:

          Calvin Cheng,谢谢。您的建议适用于 OS X 10.8.5。

          来自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)
          

          另一种解决方案 Key Listeners in python?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-04-05
            • 2018-02-13
            相关资源
            最近更新 更多