用于检测外部鼠标以打开和关闭触摸板的 UDEV 规则

用于检测外部鼠标以打开和关闭触摸板的 UDEV 规则

以下 udev 规则部分起作用,它将关闭触摸板,但如果蓝牙鼠标断开连接,它将不会重新启用触摸板。

我注意到,如果添加鼠标,xinput 会更新,这将获取 UDEV 规则的第一部分,但是 xinput才不是如果同一鼠标断开连接,则更新,例如从 xinput 列表中删除蓝牙鼠标。我在想,也许这是重新启用触摸板的问题,但不知道该怎么做?

/etc/udev/rules.d/01-touchpad.rules

SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="add", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/usr/bin/synclient TouchpadOff=1"
SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="remove", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/usr/bin/synclient TouchpadOff=0"

https://wiki.archlinux.org/index.php/Touchpad_Synaptics#Disable_touchpad_on_mouse_detection

答案1

我想为那些迫切需要 ALPS 触摸板的人添加一个编辑,它在 ubuntu 16.04 中被识别为鼠标而不是触摸板。

修改脚本如下即可:

#!/bin/sh 
# 
# Enables the touchpad if and only if there aren't any external mice connected. 
# 
# Originally from: 
#     https://wiki.archlinux.org/index.php/Touchpad_Synaptics#System_with_multiple_X_sessions

# This supports also ALPS touchpads.

#Here puts how is named (Run 'xinput list' for the name to put below)
TOUCHPAD="ALP..." 
FOUND=0 

for MOUSE in `find /sys/class/input -name mouse\*` 
do 
    if [ "`cat $MOUSE/device/name`" != "$TOUCHPAD" ] 
    then 
        FOUND=1 
  #Find a Mouse device other than the TouchPad and record the variable.
        break 
    fi 
done 

DISPLAY=:0 
export DISPLAY 
for USER in `w -h | cut -d\  -f1 | sort | uniq` 
do 
    XAUTHORITY=`sudo -Hiu $USER env | grep ^HOME= | cut -d= -f2`/.Xauthority 
    export XAUTHORITY 
    TOUCHPADDEVICE=$(($(xinput list | grep -i "$TOUCHPAD" | cut -d= -f2 | cut -d[ -f1)+0)) 
#Find the Touchpad Id
    if [ $FOUND -eq 1 ]; then
#If another device is founded, disable touchpad
        xinput disable $TOUCHPADDEVICE
    else
#else enable touchpad
        xinput enable $TOUCHPADDEVICE
    fi
done

答案2

这就是我在 Ubuntu 16.04 上的运作方式。

首先创建打开和关闭触摸板的脚本。了解触摸板的命名方式:

xinput list

例子

然后创建NAMEOFSCRIPT.sh文件

#!/bin/sh 
# 
# Enables the touchpad if and only if there aren't any external mice connected. 
# 
# Originally from: 
#     https://wiki.archlinux.org/index.php/Touchpad_Synaptics#System_with_multiple_X_sessions 

#Here puts how is named
TOUCHPAD="PS/2 Synaptics TouchPad" 
FOUND=0 

for MOUSE in `find /sys/class/input -name mouse\*` 
do 
    if [ "`cat $MOUSE/device/name`" != "$TOUCHPAD" ] 
    then 
        FOUND=1 
  #Find a Mouse device other than the TouchPad and record the variable.
        break 
    fi 
done 

DISPLAY=:0 
export DISPLAY 
for USER in `w -h | cut -d\  -f1 | sort | uniq` 
do 
    XAUTHORITY=`sudo -Hiu $USER env | grep ^HOME= | cut -d= -f2`/.Xauthority 
    export XAUTHORITY 
    TOUCHPADDEVICE=$(($(xinput list | grep -i touchpad | cut -d= -f2 | cut -d[ -f1)+0)) 

#Find the Touchpad Id
    if [ $FOUND -eq 1 ]; then
#If another device is founded, disable touchpad
        xinput disable $TOUCHPADDEVICE
    else
#else enable touchpad
        xinput enable $TOUCHPADDEVICE
    fi
done

不要忘记使其可执行!

chmod +x NAMEOFSCRIPT.sh

然后创建规则(需要root)

gksudo gedit /etc/udev/rules.d/01-touchpad_toggle.rules  

(可以是任意名字,最好指定01在开头)

SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="add", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/**$USERNAME**/.Xauthority", RUN+="**PATH TO NAMEOFSCRIPT.SH**" 
SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="remove", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/**$USERNAME**/.Xauthority", RUN+="**PATH TO NAMEOFSCRIPT.SH**" 

如果一切顺利,当我连接 USB 鼠标时,它会禁用触摸板,断开连接时,它会激活触摸板。对于那些无法识别 USB 鼠标的人,请先断开lsusb连接,然后lsusb再连接。

资料来源:https://rufflewind.com/2014-06-24/auto-disable-touchpad-linux

相关内容