如何根据鼠标是否连接来切换触控板?
我想在连接外接鼠标时自动禁用触摸板,当没有外接鼠标时启用。 我尝试过使用touchpad-indicator
但是当计算机在连接鼠标时进入睡眠状态并且鼠标断开连接时唤醒,则会失败。
我试图将以下脚本变成一个守护进程来解决这个问题,但我无法让它工作:
#!/bin/bash declare -i TID declare -i MID TID=`xinput list | grep -Eo 'Touchpad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'` MID=`xinput list | grep -Eo 'Mouse\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'` if [ $MID -gt 0 ] then xinput disable $TID else xinput enable $TID fi
我试过start-stop-daemon -S -x ./myscript.sh -b
和setsid ./myscript.sh >/dev/null 2>&1 < /dev/null &
和nohup ./myscript 0/dev/null &
甚至./myscript.sh &
所有这些都会返回一些4位数字,我想这应该是启动过程的PID,但是当我启动lxtask时,没有具有此PID的进程 ,即使我勾选“查看所有进程”。 当然,它不起作用!
您需要的基本脚本是:
#!/usr/bin/env bash ## Get the touchpad id. The -P means perl regular expressions (for \K) ## the -i makes it case insensitive (better portability) and the -o ## means print only the matched portion. The \K discards anything matched ## before it so this command will print the numeric id only. TID=$(xinput list | grep -iPo 'touchpad.*id=\K\d+') ## Run every second while : do ## Disable the touchpad if there is a mouse connected ## and enable it if there is none. xinput list | grep -iq mouse && xinput disable "$TID" || xinput enable "$TID" ## wait one second to avoind spamming your CPU sleep 1 done
上面的脚本将根据鼠标是否连接来切换触控板。 启动时,它将永远运行,并将每秒检查一次鼠标,相应地禁用或启用触摸板。
现在,将脚本保存为~/touchpad.sh
,使其可执行( chmod +x ~/touchpad.sh
)并将其添加到GUI会话启动程序中。 您尚未指定使用的桌面环境,但由于您提到了lxtask
,我假设您使用的是LXDE
。 无论如何,这里有LXDE
和Unity
:
-
将脚本添加到LXDE的自动启动文件
echo "@$HOME/touchpad.sh" >> ~/.config/lxsession/PROFILE/autostart file
确保将“PROFILE”替换为LXDE配置文件的实际名称 ,您可以通过运行
ls ~/.config/lxsession/
找到它的内容。 -
将脚本添加到Unity的自动启动文件中
打开
Startup Applications
(在仪表板中搜索“启动”)单击“添加”,然后在命令字段中将路径粘贴到脚本中:
好吧,我已经为它制定了一个udev规则,就像@terdon所说的那样,它更加清晰
因此,感谢本指南 ,我在/etc/udev/rules.d/中创建了一个“touchpad_toggle.rules”文件(需要root访问权限),并用两行填充:
SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="add", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/home/username/on.sh" SUBSYSTEM=="input", KERNEL=="mouse[0-9]*", ACTION=="remove", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/username/.Xauthority", RUN+="/home/username/off.sh"
不要忘记用您的用户名替换“用户名”!
这些on和off shell脚本的内容只是我问题中脚本的阉割版本。 示例 – off.sh:
#!/bin/bash declare -i TID TID=`xinput list | grep -Eo 'Touchpad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'` xinput disable $TID
你必须在on.sh中使用xinput enable $ TID
并且不要忘记在我的问题中添加脚本(或者@terdon建议的那个,但是没有循环)到会话自动启动,就像他在回答中告诉你的那样。
就是这样,但我必须添加一件事:
如果你有一个Synaptics触摸板(我有Elantech,所以它不适合我),你可以用一个简单的命令/usr/bin/synclient TouchpadOff=0
和1来替换你的脚本(你在RUN + =之后写的路径)