如何使USB鼠标的xinput鼠标设置持久?

我使用xinput来改变我的USB鼠标的设置 :

 xinput set-ptr-feedback 'USB Optical Mouse' 4 1 1 

拔下鼠标或重新启动后,如何使这些设置保持不变?

您可以将命令cron或添加到您的启动中,但两者都不是特别优雅。 如果我是你,我会将其添加到我的udev规则中,让系统检测事件并在需要时触发命令。

首先,我们需要鼠标供应商和产品字符串。 你可以通过lsusb找到这些。 寻找你的鼠标。 这是我的鼠标显示:

 Bus 004 Device 012: ID 1532:000f Razer USA, Ltd 

1532:000f1532是供应商, 000f是产品。

那么我们就向udev添加一条规则。 udev规则可在/lib/udev/rules.d/中找到。 你可以写自己的或者厚颜无耻,然后编辑另一个。 还有一个有用的小README,我建议你仔细阅读( cat /lib/udev/rules.d/README )。

无论你做什么,你想添加这样的规则。 请注意,我使用前面的ID来完成这项工作。

 BUS=="usb", SYSFS{idVendor}=="1532", SYSFS{idProduct}=="000f", ACTION=="add", RUN+="/usr/bin/xinput set-ptr-feedback 'USB Optical Mouse' 4 1 1" 

udev 立即接受。

注意udev在配置设备时可以自己做很聪明的事情。 您可能根本不需要xinput 。 以下是鼠标自定义配置的示例。

除了启动一个定期轮询xinput --list并在插入或移除设备时运行命令的小守护进程,我认为没有其他解决方案。

示例代码:

 #! /bin/sh -x # # xievd [INTERVAL] # # Poll `xinput` device list every INTERVAL seconds (default: 10) # and run script in ~/.xievd/${device_name}.sh when a device is # plugged-in (or pulled out). # # The device name is the same as given by `xinput --list`, with # the following transformations applied: # * any non-alphanumeric character is deleted (except: space, `_` and `-`) # * leading and trailing spaces are removed # * any sequence of 1 or more space chars is converted to a single `_` # interval=${1:-10} scripts_dir="$HOME/.xievd" if [ ! -d "$scripts_dir" ]; then echo 1>&2 "xievd: No scripts directory -- exiting." exit 1 fi state_dir="$(mktemp -t -d xievd.XXXXXX)" \ || { echo 1>&2 "xievd: Cannot create state directory -- exiting."; exit 1; } trap "rm -rf $state_dir; exit;" TERM QUIT INT ABRT process_xinput_device_list() { touch "${state_dir}/.timestamp" # find new devices and run "start" script xinput --list --short \ | fgrep slave \ | sed -r -e 's/id=[0-9]+.+//;s/[^a-z0-9 _-]//ig;s/^ +//;s/ *$//;s/ +/_/g;' \ | (while read device; do if [ ! -e "${state_dir}/${device}" ]; then # new device, run plug-in script [ -x "${scripts_dir}/${device}" ] && "${scripts_dir}/${device}" start fi touch "${state_dir}/${device}" done) # find removed devices and run "stop" script for d in "$state_dir"/*; do if [ "${state_dir}/.timestamp" -nt "$d" ]; then # device removed, run "stop" script device="$(basename $d)" [ -x "${scripts_dir}/${device}" ] && "${scripts_dir}/${device}" stop rm -f "$d" fi done } # main loop while true; do process_xinput_device_list sleep $interval sleep 1 done # cleanup rm -rf "$state_dir" 

将上面的代码保存在PATH中的某个xievd可执行文件中,将其添加到启动应用程序中,然后创建一个~/.xievd/USB_Optical_Mouse shell脚本:

 #! /bin/sh if [ "$1" = "start" ]; then xinput set-ptr-feedback 'USB Optical Mouse' 4 1 1 fi