2

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?

Élio Pereira
  • 181
  • 1
  • 14

1 Answers1

1

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
Andres Ordorica
  • 302
  • 1
  • 5
  • This seems to be the path for a solution. There's however a problem with respect to the permissions to access the devices. The following error occurred to me: "X Error of failed request: BadAccess (attempt to access private resource denied) Major opcode of failed request: 131 (XInputExtension) Minor opcode of failed request: 57 () Serial number of failed request: 19 Current serial number in output stream: 20" – Élio Pereira Aug 06 '20 at 20:36
  • Also, I want to point out a lapse: the command "xinput set-int-prop $ID "Device Enabled" 8 1" should be after the command "xdotool key Return". – Élio Pereira Aug 06 '20 at 20:37
  • After looking into this site https://wiki.archlinux.org/index.php/Libinput, I think now that the devices stated by the "xinput list" command which are not stated by the "libinput list-devices" command will not be supported. Therefore, the "xinput set-int-prop" command will only work for devices that are stated by both "xinput list" and "libinput list-devices" commands. – Élio Pereira Aug 06 '20 at 21:32
  • Put "xinput set-int-prop $ID "Device Enabled" 8 1" after "xdotool key Return" in your answer and I will accept it. – Élio Pereira Aug 06 '20 at 21:35
  • Yes, sorry I have tried it and it worked. Maybe there is a better way less technical, especially in the device ID. – Andres Ordorica Aug 06 '20 at 21:41
  • Your answer had anyway served my needs, since it allowed to lock the most important mouse and keyboard devices that I use. – Élio Pereira Aug 06 '20 at 21:50
  • The other problem may be a question for another post. – Élio Pereira Aug 06 '20 at 21:52
  • Im glad to hear it helped ! – Andres Ordorica Aug 06 '20 at 22:17