从命令行向程序发送键盘输入

时间:2022-08-22 11:54:53

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上运行的程序,这样我可以从命令行发送,例如,“控制温度t”Firefox过程和“回声”你好“\ n”一个gnome终端过程不关注这些流程和直接输入?

4 个解决方案

#1


14  

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

#2


1  

I found these two programs xmacro and xremote you 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脚本将keycode发送到Xorg + wine。

#3


1  

I've built a Ruby DSL around xdotool to simplify the focusing of windows and simulation of keyboard input. Here's an example, riffing on your original request:

我围绕xdotool构建了一个Ruby DSL,以简化windows的关注和键盘输入的模拟。这里有一个例子,在你最初的请求上:

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

#4


1  

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时必须激活窗口的问题。但是,您可以指定窗口,甚至使用xdotool来查找它。这是我试图完成的一个例子,根据你的需要进行修改。

xdotool key --window $(xdotool search --name "Spotify (Premium |Free )?- Linux Preview" | head -n1) ctrl+KP_Down

#1


14  

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

#2


1  

I found these two programs xmacro and xremote you 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脚本将keycode发送到Xorg + wine。

#3


1  

I've built a Ruby DSL around xdotool to simplify the focusing of windows and simulation of keyboard input. Here's an example, riffing on your original request:

我围绕xdotool构建了一个Ruby DSL,以简化windows的关注和键盘输入的模拟。这里有一个例子,在你最初的请求上:

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

#4


1  

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时必须激活窗口的问题。但是,您可以指定窗口,甚至使用xdotool来查找它。这是我试图完成的一个例子,根据你的需要进行修改。

xdotool key --window $(xdotool search --name "Spotify (Premium |Free )?- Linux Preview" | head -n1) ctrl+KP_Down