shellshxdotool

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


Consider the following illustrative sh script which uses xdtool to open a new tab in the terminal, change it to tab 1, and then write "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"

If I write something or click the mouse button somewhere else while the script is running, the text "Hello world" may be written where I clicked, mingled with the letters that I typed in the keyboard. I would like to lock the keyboard and mouse while the xdotool commands are executed so that this can't happen. Perhaps there's a xdotool option for that purpose, although I didn't find any. Do you have any suggestions?


Solution

  • first type xinput in terminal to list the devices. Then Select the ID of the device that you want to disable. Add lines to your script as follows:

    #!/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