Disable mouse when keyboard key is pressed in linux

1

1

I've given up trying to get my touchpad registered correctly. It wants to be an imps/2 device, and I can't spend any more afternoons trying to convince it otherwise (in fairness, it does quite well as an imps/2 device - tap to click, 3-finger tap to middle-click, no horizontal scroll but you can't have it all)

However, I cannot live with the fact that the lightest brush causes the tap to fire.

Is there a command I can use to disable the touchpad when a key has been recently pressed? xinput seems to be my friend here, device appears and can be configured as "ImPS/2 Generic Wheel Mouse"

user208769

Posted 2018-01-10T18:16:30.353

Reputation: 218

Interesting question! Does the laptop manufacturer offer any Linux drivers for this device? – Michael Frank – 2018-01-10T21:04:37.567

Not really. It's a Voyo VBOOK V3 - some windows drivers and a linux bios here http://en.myvoyo.com/xiazai/Win%20Pad/VBOOK%5FV3%5Fpentium/down/ - but nothing that would be helpful for this issue so far

– user208769 – 2018-01-11T16:51:30.967

Answers

1

I improved your solution so that there is no need for several files, there is less to grep and minor robustness improvements. In the code I use 'Logitech K400' device that I use, but you can change it. (With that device I want to disable mouse when Ctrl is down, but it is a different story and code.) I don't know how well it works for you that you disable the mouse only after key release. It might be better for you to start the disable on key press, except when it is a modifier key such as Ctrl, and enable after some delay after key release (ignoring modifier keys again).

#!/bin/bash

# Use 
# $ xinput --list
# to get the device names and test them with 
# $ xinput --list "name"

# Modify the pointer device name to match your hardware
pointer_id=$(xinput list --id-only 'pointer:Logitech K400')
# Modify the keyboard name to match your hardware
keyb_id=$(xinput list --id-only 'keyboard:Logitech K400')

# exit, if the devices are not available
[[ $pointer_id && $keyb_id ]] || exit 1

quit_jobs() {
    # terminate all running jobs, if any
    kill $(jobs -pr) 2>/dev/null || :
}

# Prepare the script for exit
revert() {
    # Terminate `xinput test` and other possible processes 
    quit_jobs
    # The pointer may be disabled
    xinput enable $pointer_id
}
trap revert EXIT

disable_pointer_temporarily() {
    xinput disable $pointer_id
    sleep 0.5
    xinput enable $pointer_id
}

xinput test $keyb_id | while read -r line; do
    if [[ $line == key\ release* ]]; then
        quit_jobs
        disable_pointer_temporarily &
    fi
done

jarno

Posted 2018-01-10T18:16:30.353

Reputation: 173

1That's a much nicer way to do it! Unfortunately the laptop became increasingly unusable, and now lies abandoned, but if I ever get around to resurrecting it this is certainly the code I will use! Thanks! – user208769 – 2020-01-27T16:18:50.973

1

My very hackish solution so far:

xinput test-xi2 --root | grep --line-buffered RawKeyRelease | while read -r line ; do pause-mouse-with-kill & sleep 0 ; done

This reports on any input passed to xorg, checks whether it's a keypress release (so I can still ctrl-click things) and disables the mouse momentarily. That's where things get even more hackish - two files are needed to prevent repeated keypresses extending the mouse timeout:

/usr/bin/pause-mouse do the actual mouse disabling:

#!/bin/bash
DEV="ImPS/2 Generic Wheel Mouse" ; xinput set-prop "$DEV" "Device Enabled" 0 && sleep 0.5 && xinput set-prop "$DEV" "Device Enabled" 1 

/usr/bin/pause-mouse-with-kill Cancel the previous mouse-disable, and restart the clock

#!/bin/bash
killall pause-mouse
pause-mouse

Nicer solutions would be preferred!

user208769

Posted 2018-01-10T18:16:30.353

Reputation: 218

0

Here is another answer. This disables given pointer device (e.g. mouse) during Alt or Ctrl key is down on given keyboard.

#!/bin/bash

# Use 'xinput list' to get the device names and id's, and use 
# 'xinput list $name' to test if you got the $name right.
# Use 'xinput test $id' to get key codes.

# Modify the pointer device name to match your hardware
pointer_id=$(xinput list --id-only 'pointer:Logitech K400')
# Modify the keyboard name to match your hardware
keyb_id=$(xinput list --id-only 'keyboard:Logitech K400')

[[ $pointer_id && $keyb_id ]] || exit 1

# Make sure to terminate precessess and to enable the pointer device
# on exit.
revert() {
    kill $(jobs -p) 2>/dev/null || :
    xinput enable $pointer_id
}
trap revert EXIT

keys_down=0
paused=0
xinput test $keyb_id | while read -r type action key rest; do
    # suppose $type is 'key'
    case $key in
        # key codes of (Left) Alt, Left Control and Right Control
        64|37|105)
            if [[ $action == press ]]
                then ((++keys_down))
                # suppose $action is 'release'
                else ((keys_down>0 ? keys_down-- : 1))
            fi
            if (( keys_down && !paused )); then
                xinput disable $pointer_id
                paused=1
            elif (( !keys_down && paused )); then
                xinput enable $pointer_id
                paused=0
            fi
        ;;
    esac
done

jarno

Posted 2018-01-10T18:16:30.353

Reputation: 173