我有连接到我的电脑鼠标G700。与此鼠标在Linux中(Ubuntu的)的问题是,该灵敏度是非常高的。我也不喜欢鼠标加速,所以我做了一个脚本,打开这个功能。该脚本看起来像这样
#!/bin/bash
# This script removes mouse acceleration, and lowers pointer speed
# Suitable for gaming mice, I use the Logitech G700.
# More info: http://www.x.org/wiki/Development/Documentation/PointerAcceleration/
xinput set-prop 11 'Device Accel Profile' -1
xinput set-prop 11 'Device Accel Constant Deceleration' 2.5
xinput set-prop 11 'Device Accel Velocity Scaling' 1.0
xinput set-prop 12 'Device Accel Profile' -1
xinput set-prop 12 'Device Accel Constant Deceleration' 2.5
xinput set-prop 12 'Device Accel Velocity Scaling' 1.0
用G700鼠标另一个问题是,它显示为在XINPUT两个不同的设备。这很可能是因为鼠标具有无线适配器,并且通常还经由USB电缆连接(用于充电)。这是我的xinput --list
输出(参见同上11和12):
$ xinput --list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ Logitech USB Receiver id=8 [slave pointer (2)]
⎜ ↳ Logitech USB Receiver id=9 [slave pointer (2)]
⎜ ↳ Logitech Unifying Device. Wireless PID:4003 id=10 [slave pointer (2)]
⎜ ↳ Logitech G700 Laser Mouse id=11 [slave pointer (2)]
⎜ ↳ Logitech G700 Laser Mouse 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)]
↳ Power Button id=7 [slave keyboard (3)]
这通常不是一个问题,因为该ID的通常是相同的。但有时鼠标改变的ID的,而这也正是我的问题的用武之地。
什么是写一个脚本/程序认定,属于从Logitech G700 Laser Mouse
输出命名xinput --list
两个清单的ID,然后使用这两个ID的顶部脚本运行命令的简单的方法?
你可以像下面这样。
if [ "$SEARCH" = "" ]; then
exit 1
fi
ids=$(xinput --list | awk -v search="$SEARCH" \
'$0 ~ search {match($0, /id=[0-9]+/);\
if (RSTART) \
print substr($0, RSTART+3, RLENGTH-3)\
}'\
)
for i in $ids
do
xinput set-prop $i 'Device Accel Profile' -1
xinput set-prop $i 'Device Accel Constant Deceleration' 2.5
xinput set-prop $i 'Device Accel Velocity Scaling' 1.0
done
因此,与你的第一次找到所有匹配搜索模式$SEARCH
并将其存储在$ids
,其中,ID。然后,你遍历所有的ID和执行三个xinput
命令。
你应该确保$SEARCH
不匹配得多,因为这可能会导致意外的行为。
如果设备名称是始终不变的,在这种情况下Logitech G700 Laser Mouse
,你可以搜索匹配运行的设备ID
xinput list --id-only 'Logitech G700 Laser Mouse'
我的2美分的罗技游戏鼠标G502
#!/bin/sh
for id in `xinput --list|grep 'Logitech Gaming Mouse G502'|perl -ne 'while (m/id=(\d+)/g){print "$1\n";}'`; do
# echo "setting device ID $id"
notify-send -t 50000 'Mouse fixed'
xinput set-prop $id "Device Accel Velocity Scaling" 1
xinput set-prop $id "Device Accel Constant Deceleration" 3
done
我做到了像拉斐尔阿伦斯的答案,但使用grep和sed的,而不是awk和该命令是现在像my_script part_of_device_name part_of_property_name_(含\空间空格)值:
#!/bin/sh
DEVICE=$1
PROP=$2
VAL=$3
DEFAULT="Default"
if [ "$DEVICE" = "" ]; then
exit 1
fi
if [ "$PROP" = "" ]; then
exit 1
fi
if [ "$VAL" = "" ]; then
exit 1
fi
devlist=$(xinput --list | grep "$DEVICE" | sed -n 's/.*id=\([0-9]\+\).*/\1/p')
for dev in $devlist
do
props=$(xinput list-props $dev | grep "$PROP" | grep -v $DEFAULT | sed -n 's/.*(\([0-9]\+\)).*/\1/p')
for prop in $props
do
echo $prop
xinput set-prop $dev $prop $VAL
done
done
对于它的乐趣,同样的答案,但更简单的方法来分析,并得到IDS:
for id in $(xinput list | grep 'Logitech USB Receiver' | grep pointer | cut -d '=' -f 2 | cut -f 1); do xinput --set-button-map $id 3 2 1; done
我花了一段时间才能找出这个可以得到IDS:
xinput | cut -d '=' -f 2 | cut -f 1
目前,我在工作的脚本,针对一问题在askubuntu.com,这需要类似的东西,我想我会分享了简单的Python脚本,它几乎是这个问题问的东西 - 找到设备ID和设置属性:
from __future__ import print_function
import subprocess
import sys
def run_cmd(cmdlist):
""" Reusable function for running shell commands"""
try:
stdout = subprocess.check_output(cmdlist)
except subprocess.CalledProcessError as pserror:
sys.exit(1)
else:
if stdout:
return stdout.decode().strip()
def list_ids(mouse_name):
""" Returns list of ids for the same device"""
while True:
mouse_ids = []
for dev_id in run_cmd(['xinput','list','--id-only']).split('\n'):
if mouse_name in run_cmd(['xinput','list','--name-only',dev_id]):
mouse_ids.append(dev_id)
if mouse_ids:
break
return mouse_ids
"""dictionary of propery-value pairs"""
props = { 'Device Accel Profile':'-1',
'Device Accel Constant Deceleration':'2.5',
'Device Accel Velocity Scaling':'1.0' }
""" set all property-value pair per each device id
Uncomment the print function if you wish to know
which ids have been altered for double-checking
with xinput list-props"""
for dev_id in list_ids(sys.argv[1]):
# print(dev_id)
for prop,value in props.items():
run_cmd(['xinput','set-prop',dev_id,prop,value])
提供鼠标作为第一个命令行参数的报价名称:
python set_xinput_props.py 'Logitech G700 Laser Mouse'
如果一切正常,脚本默默退出,与0
的退出状态,或者1
如有xinput
命令失败。您可以取消print
声明显示正在配置哪个ID(与xinput
后来仔细检查值正常的设定)
从本质上讲,list_ids
功能列出了所有的设备ID,发现那些具有相同的名称作为用户的鼠标名称设备,并返回这些ID的列表。接下来,我们简单地遍历他们中的每一个,每一个我们都设置在props
字典中定义的属性值对。可以与元组的名单进行为好,但字典是我选择在这里。