2

我正在编写一个程序来自动化电子邮件过程,并想知道是否有办法从程序中运行击键?

例如说我有这个字符串:

str = "test"

它被复制到一个文件中:

File.open('str.txt', 'w') { |s| s.puts(str }

之后我想使用CNTRL-A; CNTRL-C在文件上并复制信息,这在 Ruby 程序中是否可能,而不使用外部 gem?

操作系统:Windows 7

4

2 回答 2

2

如果您可以使用 gem https://github.com/erinata/auto_click来向其他应用程序发送任意击键。但是,如果您不能使用 gems,您可以做的是使用适当的命令行参数运行NirCmd(或其替代方案之一 实现相同的结果。

例如:

# Tell the OS to bring up the Notepad window and give it the time
# to do so.
`nircmd win activate ititle notepad`
sleep 0.5
# Select all text in the Notepad window and copy it to the
# clipboard.
`nircmd sendkeypress ctrl+a`
`nircmd sendkeypress ctrl+c`
于 2016-05-08T09:14:46.943 回答
2

如果您无法安装 gems 但可以复制ClipText.exe到当前目录,请执行此操作,然后在 Ruby 中运行以下代码:

File.open('str.txt', 'w') { |s| s.puts(str }
`cliptext.exe from str.txt`

有关在 Windows 上执行命令的更严格方法,请参阅“如何在 Ruby 中执行 Windows CLI 命令? ”。

于 2016-05-06T18:21:55.280 回答