View Poll Results: Did this work for you?

Voters
23. You may not vote on this poll
  • Yes!

    17 73.91%
  • No.

    6 26.09%
Page 1 of 2 12 LastLast
Results 1 to 10 of 32

Thread: [HOWTO] Automatically disable touchpad when external mouse connected

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    [HOWTO] Automatically disable touchpad when external mouse connected

    Introduction:
    If you're like me, you hate using your touchpad when you could be using an external mouse. If you're like me you also have a habit of disabling your touchpad when using a mouse, and forgetting to re-enable it until after you unplug your mouse. If you're like me, this tutorial is exactly what you need.

    What you need to get started:
    You will need to make sure that you have xinput and halevt installed. You almost certainly already have xinput, but the following command will make sure you have what you need:
    Code:
    sudo apt-get install xinput halevt
    I also recommend that you create a folder for scripts (if you haven't already), and add it to your $PATH. I created a "bin" folder in my home directory for this. Create the folder with:
    Code:
    mkdir ~/bin
    and add it to your path by issuing the following commands:
    Code:
    echo "PATH=\$PATH:~/bin" >> ~/.bashrc
    echo "export PATH" >> ~/.bashrc
    Configure the script:
    First we need to create and configure the script which will actually toggle the touchpad:
    Code:
    cd ~/bin
    touch toggleTouchpad
    gedit toggleTouchpad
    The script should now be open in gedit (feel free to replace gedit with your favorite text editor). Paste the following into the script:
    Code:
    # toggleTouchpad by Brendon Dugan
    # Toggles a touchpad on or off depending on it's current state or CLI argument
    #
    # To configure, run the command 'xinput list' in terminal and identify your touch pad.
    # Using the output of the above command, change the touchpadString variable to a substring
    # of your touchpad's description that is unique to that device.
    #
    # To run, simply type 'toggleTouchpad' to toggle your touchpad on or off, or
    # 'toggleTouchpad on' to explicitly turn your touchpad on, or
    # 'toggleTouchpad off' to explicitly turn it off.
    #
    # Enjoy!
    
    
    # A function for logging
    safemk () {
    if [ ! -d $1 ]; 
      then mkdir $1; 
      chmod +rw $1; 
    fi
    }
    logdir=/home/$USER/.toggleTouchpad
    touchpadString="TouchPad"
    touchpadID=$(xinput list | grep $touchpadString | awk -F " " '{print $6}' | awk -F "=" '{print $2}')
    touchpadEnabled=$(xinput list-props $touchpadID | grep "Device Enabled" | awk -F ":" '{print $2}')
    sleeptime=1
    
    # Create the logging directory
    safemk $logdir
    touch $logdir/errorLog.txt
    
    # Check for arguments on the command line
    if test $# -eq 1
    then
    	# Change the argument to lowercase
    	arg1=$(echo $1 | tr [:upper:] [:lower:])
    	cliArg=1
    else
    	# There is no argument.
    	cliArg=0
    fi
    
    if [ $cliArg -eq 1 ]
    then
    	# If there's an argument, check to see whether it is on, off, or junk
    	if [ $arg1 = 'on' ]
    	then
    		# The argument was 'on', so turn the touchpad on
    		xinput --set-prop $touchpadID "Device Enabled" 1
    		if [ $(xinput list-props $touchpadID | grep "Device Enabled" | awk -F ":" '{print $2}') -eq 0 ]
    		then
    			echo "Something went wrong\n" >> $logdir/errorLog.txt
    		fi
    	elif [ $arg1 = 'off' ]
    	then
    		# Sleep for a short time to fix a bug that re-enabled the touchpad immediately after disabling it
    		sleep $sleeptime
    		# The argument was 'off', so turn the touchpad off 
    		xinput --set-prop $touchpadID "Device Enabled" 0
    		if [ $(xinput list-props $touchpadID | grep "Device Enabled" | awk -F ":" '{print $2}') -eq 1 ]
    		then
    			echo "Something went wrong, perhaps \$sleeptime needs to be greater than $sleeptime ?\n" >> $logdir/errorLog.txt
    		fi
    	else
    		# The argument was junk, so log the error and go on 
    		echo "Invalid argument \""$arg1"\" was supplied\n" >> $logdir/errorLog.txt
    	fi
    else
    	# There was no argument, so just toggle the touchpad to the opposite
    	# of the state it has now.
    	if [ $touchpadEnabled -eq 1 ]
    	then
    		xinput --set-prop $touchpadID "Device Enabled" 0
    	else
    		xinput --set-prop $touchpadID "Device Enabled" 1
    	fi
    fi
    Now make the script executable by running:
    Code:
     chmod +x ~/bin/toggleTouchpad
    Ok, we're almost configured. We need to make sure that your touchpad will be affected by the script. Run the following command to get a list of all current input devices:
    Code:
    xinput -list
    It should have an output something like this:
    Code:
    brendon@brendon-lappy-linux:~$ xinput -list
    ⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
    ⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
    ⎜   ↳ Logitech Logitech BT Mini-Receiver      	id=15	[slave  pointer  (2)]
    ⎜   ↳ SynPS/2 Synaptics TouchPad              	id=12	[slave  pointer  (2)]
    ⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
        ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
        ↳ Power Button                            	id=6	[slave  keyboard (3)]
        ↳ Video Bus                               	id=7	[slave  keyboard (3)]
        ↳ Power Button                            	id=8	[slave  keyboard (3)]
        ↳ Sleep Button                            	id=9	[slave  keyboard (3)]
        ↳ Laptop_Integrated_Webcam_2M             	id=10	[slave  keyboard (3)]
        ↳ AT Translated Set 2 keyboard            	id=11	[slave  keyboard (3)]
        ↳ Dell WMI hotkeys                        	id=13	[slave  keyboard (3)]
        ↳ Logitech Logitech BT Mini-Receiver      	id=14	[slave  keyboard (3)]
    brendon@brendon-lappy-linux:~$
    If your touchpad has the word "TouchPad" (case sensitive) in it, the script is ready to go. If it doesn't, edit the variable "touchpadString" in the script to match your touchpad... but remember everything is case sensitive. For now, your script is configured. Next step is testing.

    Test the script:
    Make sure your touchpad is working, and then open a new terminal window. We are going to do four tests. Before and after each test, try your touchpad.

    Test 1:
    Code:
    toggleTouchpad
    Did your touchpad stop working? Good!

    Test 2:
    Code:
    toggleTouchpad
    Did your touchpad start working again? Good!

    Test 3:
    Code:
    toggleTouchpad off
    Wait at least one second...
    Did your touchpad stop working? Good!

    Test 4:
    Code:
    toggleTouchpad on
    Wait at least one second...
    Did your touchpad start working again? Good!

    Making the magic happen automatically:
    We're almost there! Now we need to set the script to run automatically when your mouse is plugged in. Making sure your mouse is unplugged, run the following command:
    Code:
    halevt -i >>~/connectedDevices.txt
    While the command is running, plug in your mouse, and then unplug it. Now press Ctrl +c to kill the process. Open ~/connectedDevices.txt, and you should see something that looks like:
    Code:
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_b02_noserial
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_b02_noserial_if0
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023_if0
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023_if0_logicaldev_input
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_c70a_00076142E023
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_c70a_00076142E023_if0
    New Device: /org/freedesktop/Hal/devices/usb_device_46d_c70a_00076142E023_if0_logicaldev_input
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023_if0_logicaldev_input
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023_if0
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_c70a_00076142E023_if0_logicaldev_input
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_b02_noserial_if0
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_c70a_00076142E023_if0
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_c70a_00076142E023
    Device removed: /org/freedesktop/Hal/devices/usb_device_46d_b02_noserial
    All of those devices are your mouse. Since each of those events will trigger every time you plug in your mouse, we only need to handle one of them. Pick one that ends in seemingly random numbers, and copy and paste everything after the ":" into a text file. We will be using it in a moment. Now, let's create our halevt config file:
    Code:
    touch ~/.halevt.xml && gedit ~/.halevt.xml
    Paste the following into the file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <halevt:Configuration version="0.1" xmlns:halevt="http://www.environnement.ens.fr/perso/dumas/halevt.html">
    	<halevt:Device match="hal.info.udi = /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023">
    		<halevt:Insertion exec="toggleTouchpad off"/>
    		<halevt:Removal exec="toggleTouchpad on"/>
    	</halevt:Device>
    </halevt:Configuration>
    We are almost done! Remember the bit I had you paste into another file?? We are going to use that to identify your device. In the config file, change the line that says:
    Code:
    <halevt:Device match="hal.info.udi = /org/freedesktop/Hal/devices/usb_device_46d_c70e_00076142E023">
    to say :
    Code:
    <halevt:Device match="hal.info.udi = /org/freedesktop/Hal/devices/the_rest_of_what_you_copied_into_the_file">
    Theoretically, we're done!

    Test the Magic:
    From terminal run:
    Code:
    sudo killall halevt
    halevt -c ~/.halevt.xml
    Now, connect your mouse. If all is going well, about ~1.5 seconds after you plug in your mouse your touchpad should stop working. Now, disconnect your mouse. Your touchpad should start working again.

    Making it permanent:
    If all went well in the tests, you will want to make this happen automatically forever. Go to "System->Preferences->Startup Applications", and add a new startup program. Name it something you will remember, and for the command put "halevt -c ~/.halevt.xml". You're done!!

    Didn't work? Undo it!:
    Undoing this configuration is fairly straightforward. Start out by going to "System->Preferences->Startup Applications" and removing the entry we added in the step above. Now let's remove halevt and the files we created:
    Code:
    sudo apt-get remove halevt
    rm ~/.halevt.xml ~/bin/toggleTouchpad
    Optionally you can also remove the ~/bin folder (it's still a good idea to have in my opinion) and the configuration for it:
    Code:
    rmdir ~/bin
    gedit ~/.bashrc
    and remove the lines
    Code:
    PATH=\$PATH:~/bin
    export PATH
    from the end. You should be set back to your normal state!

    Special Thanks:
    While writing this script I received a great deal of help from the user Arndt, so you should go leave him lots of love and thanks.
    Last edited by brennydoogles; November 26th, 2010 at 09:52 PM. Reason: Changed all aptitude references to apt-get, added a section for reverting changes, changed rm ~/bin to rmdir ~/bin.
    Registered Linux user 446122 , Registered Machine 352936.

  2. #2
    Join Date
    Jul 2008
    Location
    Canada
    Beans
    8
    Distro
    Ubuntu

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    Wow, thank you so much, this worked perfectly, even better it gave me a way to turn off my touchpad easily .

    One tiny little thing in your tutorial though: In the Undo the Changes section: the rm ~/bin line.
    rm doesn't remove directories and rm -r could be problematic
    rm ~/bin/toggleTouchpad
    rmdir ~/bin

    removes the toggleTouchpad, and only removes ~/bin if its empty.

    Also, is there a way to turn the touchpad on/off (automatically) when you turn the on/off the mouse's power.

  3. #3
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    Quote Originally Posted by brishu View Post
    Wow, thank you so much, this worked perfectly, even better it gave me a way to turn off my touchpad easily .

    One tiny little thing in your tutorial though: In the Undo the Changes section: the rm ~/bin line.
    rm doesn't remove directories and rm -r could be problematic
    rm ~/bin/toggleTouchpad
    rmdir ~/bin

    removes the toggleTouchpad, and only removes ~/bin if its empty.
    I'm glad to help! You are absolutely right about the rmdir, I was not thinking when I wrote that section, and it has been edited to be correct now.

    Quote Originally Posted by brishu View Post
    Also, is there a way to turn the touchpad on/off (automatically) when you turn the on/off the mouse's power.
    It would depend on the mouse and what kinds of events occur when you turn on/off your device. Running
    Code:
    halevt -i
    after your mouse is connected and then turning it on and off should print to the screen any events triggered. For my mouse (Logitech MX1000) I seem not to get anything. If you do get something, post the output and I will try my best to help you rewrite your rule for that even instead of the "connected" event.
    Registered Linux user 446122 , Registered Machine 352936.

  4. #4
    Join Date
    Jul 2008
    Location
    Canada
    Beans
    8
    Distro
    Ubuntu

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    No output (Logitech M305) , but thanks anyway (Plus, now I can turn the touchpad on/off with a shortcut so its not that big of a deal. thanks)
    Last edited by brishu; November 26th, 2010 at 11:53 PM.

  5. #5
    Join Date
    Feb 2008
    Location
    Warrnambool, Australia
    Beans
    8
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    Wonderful! Works with my AlpsPS/2 ALPS GlidePoint touchpad. (Changed touchpadString="GlidePoint")

    This is a great example of what I like about linux:
    1. That it is possible for you can control your computer as well as this.
    2. Someone in the community will figure out how it is done and then shares what they learnt with others.
    3. It is not just a program to download - I learn more about computing each time.


    Thanks very much!

  6. #6
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    Quote Originally Posted by deafiant View Post
    Wonderful! Works with my AlpsPS/2 ALPS GlidePoint touchpad. (Changed touchpadString="GlidePoint")

    This is a great example of what I like about linux:
    1. That it is possible for you can control your computer as well as this.
    2. Someone in the community will figure out how it is done and then shares what they learnt with others.
    3. It is not just a program to download - I learn more about computing each time.


    Thanks very much!
    Glad it works! I agree about why you like Linux so much, the community and ability to share my work (as well as reaping the benefits of everyone else's work) is one of the main things that keeps renewing my interest.
    Registered Linux user 446122 , Registered Machine 352936.

  7. #7
    Join Date
    Jan 2010
    Location
    Mexico City
    Beans
    65
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    Neat code! Mega thanx 2 brennydoogles, works fine in Dell XPS 13 corei5 running 64bit Precise (12.04) Ubuntu.

  8. #8
    Join Date
    Sep 2011
    Beans
    9

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    Hi.

    The script is not working for me. xinput command shows that I have two touchpads, here's the output from terminal window:

    Code:
    Virtual core pointer                    	id=2	[master pointer  (3)]
    ⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
    ⎜   ↳ Logitech Gaming Mouse G400              	id=10	[slave  pointer  (2)]
    ⎜   ↳ X10 WTI RF receiver mouse               	id=11	[slave  pointer  (2)]
    ⎜   ↳ SynPS/2 Synaptics TouchPad              	id=14	[slave  pointer  (2)]
    ⎜   ↳ SynPS/2 Synaptics TouchPad              	id=15	[slave  pointer  (2)]
    ⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
        ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
        ↳ Power Button                            	id=6	[slave  keyboard (3)]
        ↳ Video Bus                               	id=7	[slave  keyboard (3)]
        ↳ Sleep Button                            	id=8	[slave  keyboard (3)]
        ↳ Bison Webcam                            	id=9	[slave  keyboard (3)]
        ↳ X10 WTI RF receiver                     	id=12	[slave  keyboard (3)]
        ↳ AT Translated Set 2 keyboard            	id=13	[slave  keyboard (3)]
    Can somebody please explain or point how can I delete one of two touchpads? Or maybe put some changes to the script so it can work with my configuration.

  9. #9
    Join Date
    Sep 2011
    Beans
    9

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    bump

  10. #10
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [HOWTO] Automatically disable touchpad when external mouse connected

    It would be a good idea to create a new thread about your problem. This thread is from 2010 and the last reply was last year.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •