我需要能够以编程方式在特定坐标处触发鼠标单击。我发现 AutoIt 和 auto_click gem 都应该提供这种能力,但仅限于 Windows。我还发现了 rautomation gem,它旨在提供跨平台功能,但目前似乎不支持 Windows 以外的任何东西。
是否有任何其他 gem 允许直接从 Ruby 自动点击特定 x/y 坐标的鼠标?
最佳答案
我认为这是一项严重依赖系统的任务。您应该为您的代码提供一种加载系统相关 gem 的方法(Win 上的 AutoIt,Linux 上的 Automations)。如果您的目标是 Mac OS,您可以通过 FFI 库从 CGRemoteOperation.h
调用 CGPostMouseEvent
来构建您自己的库。
例如:
需要'ffi'
module Mouse
extend FFI::Library
ffi_lib '/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics'
class CGPoint < FFI::Struct
layout :x, :double, :y, :double
end
attach_function :CGPostMouseEvent, [ CGPoint, :bool, :int, :bool ], :void
end
point = Mouse::CGPoint.new
point[:x] = 100
point[:y] = 100
Mouse::CGPostMouseEvent(point, true, 1, true)
关于ruby - 是否有支持跨平台鼠标操作的 ruby gem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7558315/