【问题标题】:Is there a ruby gem that enables cross-platform mouse manipulation?是否有支持跨平台鼠标操作的 ruby​​ gem?
【发布时间】:2011-11-25 09:03:34
【问题描述】:

我需要能够以编程方式在特定坐标处触发鼠标点击。我发现 AutoIt 和 auto_click gem 都应该提供这种能力,但只在 Windows 上。我还发现了 rautomation gem,它旨在提供跨平台功能,但目前似乎不支持 Windows 以外的任何东西。

是否有任何其他 gem 允许直接从 Ruby 自动在特定 x/y 坐标上单击鼠标?

【问题讨论】:

标签: ruby automation


【解决方案1】:

我认为这是一项严重依赖系统的任务。您应该为您的代码提供一种加载与系统相关的 gem 的方法(Win 上的 AutoIt,Linux 上的自动化)。如果您的目标是 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)

【讨论】:

    【解决方案2】:

    看看rumouse gem。提供简单的API,支持linux、osx、windows。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      相关资源
      最近更新 更多