在mac终端中模拟键盘按下
我正在运行一些模拟,需要手动键盘输入来更改参数(令人烦恼)。
有没有办法模拟键盘按下,以便我可以使用 bash 脚本运行模拟?
I'm running some simulations which require manual keyboard input to change the parameters (annoyingly).
Is there a way to simulate keyboard presses, so that I can run the simulations with a bash script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然我不推荐它,但您可以执行类似的操作(它仅列出您的主目录的内容)
但是这相当于训练猫在键盘上行走。该代码不知道终端中发生了什么。它只是“输入”一些内容并按回车键,完全不经意间。
因此,如果您有任何其他方式将输入传递给流程,请改用它。我刚刚发布了这个,因为您确实要求 AppleScript 解决方案。我只是怀疑 AppleScript 是正确的解决方案。
While I won't recommend it, you can do something like this (it just lists your home directory's contents)
However this is the digital equivalent of training a cat to walk on your keyboard. The code has no clue what's going on in the terminal. It just "types" something and presses return, completely obliviously.
So if you have any other way of passing the input to the process, use that instead. I just posted this, since you did ask for an AppleScript solution. I just doubt that AppleScript's the right solution.
也许你可以使用
expect
?还有 echo |。
Maybe you could use
expect
?Also
echo | <cmd>
.事实证明我一直可以从 bash 做:
Turns out I could do from bash all along:
我是 cli-driver 的作者,它是一个 Node.js 库自动化命令行,类似于网络驱动程序,但在命令行中。它支持 mac、linux 和 windows,到目前为止,我能够轻松地“自动化”我需要的任何用例,从复杂的提示,到多个命令任务、ssh 会话、使用 tab 完成命令或使用 control+r 执行历史记录,甚至使用vi、nano、emacs。
它可以让您实际创建一个新终端并模拟用户输入(仅键盘)。一般来说,您需要模拟人类用户的击键(如字符),但在特定序列中,如“enter”、“shift-tab”、“control-q”、“control-shift-right”等。但更一般地说,任何 ansi 序列都可以编写用于控制光标、键盘和显示功能 - 尽管通常您不需要这些。
它在内部使用 node-pty,它是“node.js 的 forkpty(3) 绑定。这允许您使用伪终端文件描述符来分叉进程。它返回一个允许读取和写入的终端对象。”
不过,通信只是一种方式 - 输入文本并等待输出,例如(主页上有很多):
I'm the author of cli-driver which is is a Node.js library to automate the command line, the analogy to web-driver but in the command line. It supports mac, linux and windows and so far I was able to easily "automate" any use case I needed, from complex prompts, to multiple command tasks, ssh sessions, command completion with tab or history with control+r, or even using vi, nano, emacs.
It let you actually create a new terminal and simulate user input (keyboard only) . In general you need to simulate a human user keystrokes like characters but in particular sequences like "enter", "shift-tab", "control-q", "control-shift-right", etc. But more generally any ansi sequence can written to control the cursor, keyboard and display capabilities - although in general you don't need that.
Internally it uses node-pty which is "forkpty(3) bindings for node.js. This allows you to fork processes with pseudoterminal file descriptors. It returns a terminal object which allows reads and writes."
The comunicacion is one way only though - you enter text and wait for an output, example (plenty on the home page):
我喜欢使用 robotsgo 来控制机器的各个方面: https://github.com/go-vgo/机器人Go
I like using robotgo to control all aspects of your machine: https://github.com/go-vgo/robotgo