通过 sh 脚本 运行 xdotool 命令时锁定键盘和鼠标

Lock keyboard and mouse while running xdtool commands through a sh script

考虑以下说明性 sh 脚本,它使用 xdtool 在终端中打开一个新选项卡,将其更改为选项卡 1,然后写入“Hello world”:

#!/bin/sh
cd ${0%/*} || exit 1 # run from this directory

xdotool key ctrl+shift+t                                   #Open new terminal tab

xdotool key alt+1                                          #Switch to the tab 1 of the terminal

xdotool type "echo \"Hello world\""                        #Write something

xdotool key Return                                         #Press "Enter"

如果我在脚本处于 运行 时在其他地方写东西或单击鼠标按钮,文本“Hello world”可能会写在我单击的地方,与我在键盘上键入的字母混合在一起。我想在执行 xdotool 命令时锁定键盘和鼠标,以免发生这种情况。也许有一个用于该目的的 xdotool 选项,尽管我没有找到任何选项。你有什么建议吗?

首先在终端中输入 xinput 以列出设备。然后 Select 您要禁用的设备的 ID。将行添加到您的脚本中,如下所示:

#!/bin/sh

export DISPLAY=:0
xinput set-int-prop $ID "Device Enabled" 8 0

cd ${0%/*} || exit 1 # run from this directory

xdotool key ctrl+shift+t                                   #Open new terminal tab

xdotool key alt+1                                          #Switch to the tab 1 of    the terminal

xdotool type "echo \"Hello world\""                        #Write something

 xinput set-int-prop $ID "Device Enabled" 8 1

xdotool key Return