Linux 从命令行向程序发送键盘输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3792487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending keyboard input to a program from command-line
提问by OTZ
How do you send keyboard input to a program?
如何将键盘输入发送到程序?
That is, under a Linux GUI, is there a good manual (programmable) way, or tool, of simulating keyboard input on a running program on Linux, so that I can send from the command-line, e.g., "Control-T" to a Firefox process and "echo 'hello'\n" to a Gnome-Terminal process without actually focusing on each of those processes and typing in directly?
也就是说,在 Linux GUI 下,是否有一种很好的手动(可编程)方式或工具来模拟 Linux 上正在运行的程序上的键盘输入,以便我可以从命令行发送,例如“Control-T”到 Firefox 进程和“echo 'hello'\n”到 Gnome-Terminal 进程而不实际关注每个进程并直接输入?
采纳答案by OTZ
xdotool
does have a way of sending keystrokes if limited to a focused window:
xdotool
如果仅限于聚焦窗口,确实有一种发送击键的方法:
WID=`xdotool search "Mozilla Firefox" | head -1`
xdotool windowactivate $WID
xdotool key ctrl+l
回答by jcubic
I found these two programs xmacroand xremoteyou may take a look, but it seems that they're not well documented.
我发现这两个程序xmacro和xremote你可以看看,但似乎它们没有很好的记录。
I also found this utility xvkbd
in answer to this question SO Q&A titled: Sending keycode to Xorg + wine with bash script.
我还在xvkbd
回答这个问题时发现了这个实用程序,所以问答题为: 使用 bash 脚本将密钥代码发送到 Xorg + wine。
回答by nsxt
I've built a Ruby DSLaround xdotool
to simplify the focusing of windows and simulation of keyboard input. Here's an example, riffing on your original request:
我已经建立了一个Ruby的DSL围绕xdotool
简化窗口和键盘输入的模拟的聚焦。这是一个示例,重复您的原始请求:
Mani.new(window_manager: :xmonad) do
window :hello, launch: 'urxvt' do
run 'echo "hello\n"'
end
window :firefox, launch: 'firefox', delay: 1.5 do
type '{{ctrl+t}}'
end
end
回答by Chlorek
It's an old topic, but one still may be looking for this, someone mentioned here solution where window must be activated when using xdotool. However you can specify window and even use xdotool to find it. Here is example I tried to accomplish, change it as you need.
这是一个古老的话题,但人们可能仍在寻找这个,有人在这里提到了使用 xdotool 时必须激活窗口的解决方案。但是,您可以指定 window 甚至使用 xdotool 来查找它。这是我尝试完成的示例,请根据需要进行更改。
xdotool key --window $(xdotool search --name "Spotify (Premium |Free )?- Linux Preview" | head -n1) ctrl+KP_Down