Python - OS X 中的屏幕抓取和控制鼠标

发布于 2024-10-08 20:58:12 字数 928 浏览 1 评论 0原文

我正在研究 OS X 中的屏幕抓取和控制鼠标的爱好项目。

我并不是在寻找最优雅的方式,但我需要能够每半秒左右捕获一次屏幕。

我发现我可以使用 screencapture 命令行工具 (screencapture -w -W -i ~/Desktop/capture.jpg),但我很担心它可能不够快。

我也在寻找一种发送点击、设置光标位置和获取光标位置的方法。有点像 win32api 提供的:mouse_eventSetCursorPosGetCursorPos

我发现这个示例代码使用 PyObjC 库来设置光标位置,但它总是将我的鼠标移动到 (0,0) 而不是我传递给它的坐标。

import objc

class ETMouse():   
    def setMousePosition(self, x, y):
        bndl = objc.loadBundle('CoreGraphics', globals(),
                '/System/Library/Frameworks/ApplicationServices.framework')
        objc.loadBundleFunctions(bndl, globals(),
                [('CGWarpMouseCursorPosition', 'v{CGPoint=ff}')])
        CGWarpMouseCursorPosition((x, y))

if __name__ == "__main__":
    et = ETMouse()
    et.setMousePosition(500, 500)

编辑:如果重要的话,我正在运行 Snow Leopard (10.6)。

谢谢!

I'm looking into screen-scraping and controlling the mouse in OS X for a hobby project.

I'm not looking for the most elegant way, but I need to be able to capture the screen every half a second or so.

I've found that I could use the screencapture command-line tool (screencapture -w -W -i ~/Desktop/capture.jpg), but I'm worried that it might not be fast enough.

I'm also looking for a way to send clicks, set the cursor position and obtain the cursor position. Sort of like what win32api provides: mouse_event, SetCursorPos and GetCursorPos.

I've found this sample code that uses the PyObjC library to set the cursor position, but it's always moving my mouse to (0,0) instead of the coordinates I pass it.

import objc

class ETMouse():   
    def setMousePosition(self, x, y):
        bndl = objc.loadBundle('CoreGraphics', globals(),
                '/System/Library/Frameworks/ApplicationServices.framework')
        objc.loadBundleFunctions(bndl, globals(),
                [('CGWarpMouseCursorPosition', 'v{CGPoint=ff}')])
        CGWarpMouseCursorPosition((x, y))

if __name__ == "__main__":
    et = ETMouse()
    et.setMousePosition(500, 500)

Edit: I'm running Snow Leopard (10.6) if it matters.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追星践月 2024-10-15 20:58:12

看看autopy。适用于 OSX、Linux 和 Windows。

Take a look at autopy. Works in OSX, Linux and Windows.

韵柒 2024-10-15 20:58:12

如果您只是进行屏幕抓取,我可以建议您尝试 Sikuli 吗?它让界面自动化就像儿戏一样,脚本使用Python。

If you're just screenscraping, might I suggest you try Sikuli? It makes interface automation like child's play, and the scripting uses Python.

懒的傷心 2024-10-15 20:58:12

ControllerMate 这样的自动化软件会有帮助吗?

允许您自定义行为
您的 HID 设备 — 键盘、
键盘、鼠标、轨迹球、操纵杆、
游戏手柄、油门等

Would automation software like ControllerMate help?

allows you to customize the behavior
of your HID devices — keyboards,
keypads, mice, trackballs, joysticks,
gamepads, throttles, among others

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文