如何使用Ruby将键盘和鼠标命令发送到底层操作系统? [英] How to send keyboard and mouse commands to the underlying operating system using Ruby?

查看:69
本文介绍了如何使用Ruby将键盘和鼠标命令发送到底层操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby是否有一种与操作系统无关的方式将键盘和鼠标事件发送到底层操作系统?

Is there an operating system neutral way for Ruby to send keyboard and mouse events to the underlying operating system?

(对我而言)一种显而易见的方法是使用Ruby/Java绑定并使用java.awt.Robot,但这似乎很愚蠢.

One obvious (to me) approach is to use the Ruby/Java bindings and use java.awt.Robot, but that just seems silly.

推荐答案

对于Mac:

gem install rb-appscript

然后您可以使用如下脚本进行测试:

Then you can test it with a script like this:

require "rubygems"
require "appscript"
include Appscript

app("TextEdit").activate
app("System Events").keystroke("Look Ma, keystrokes!")

对于Windows:(未经测试,从该线程借来的)

require "win32ole"

wsh = WIN32OLE.new("WScript.Shell")
wsh.Run("Notepad.exe")
while not wsh.AppActivate("Notepad")
  sleep .1
end
wsh.SendKeys("Look Ma, keystrokes!")

这篇关于如何使用Ruby将键盘和鼠标命令发送到底层操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆