如何使程序找到xinput设备的id并设置xinput一些设置

Fil*_* S. 18 shell xinput

我有一台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
Run Code Online (Sandbox Code Playgroud)

G700鼠标的另一个问题是它在xinput中显示为两个不同的设备.这很可能是因为鼠标有无线适配器,并且通常也通过USB电缆连接(用于充电).这是我的输出xinput --list(参见id 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)]
Run Code Online (Sandbox Code Playgroud)

这通常不是问题,因为id通常是相同的.但有时鼠标的id会发生变化,这就是我的问题所在.

编写脚本/程序的最简单方法是找到属于Logitech G700 Laser Mouse输出中命名的两个列表的id,xinput --list然后使用这两个id运行顶部脚本中的命令?

Rap*_*ens 14

您可以执行以下操作.

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
Run Code Online (Sandbox Code Playgroud)

因此,首先要找到与搜索模式匹配的所有ID并将其$SEARCH存储在其中$ids.然后循环ID并执行三个xinput命令.

你应该确保它$SEARCH不匹配,因为这可能导致不希望的行为.


小智 10

如果设备名称始终相同,则在这种情况下Logitech G700 Laser Mouse,您可以通过运行来搜索匹配的设备ID

xinput list --id-only 'Logitech G700 Laser Mouse'
Run Code Online (Sandbox Code Playgroud)

  • 这不适用于我的罗技鼠标(`xinput list --id-only pointer:"Logitech G203 Prodigy Gaming Mouse"`)。xinput 拒绝打印 ID,因为“有多个设备匹配”。虽然有点傻。 (5认同)
  • 由于某种原因,添加标签“pointer:”有效,换句话说,“xinput list --id-onlypointer:'the name'”可以正确打印鼠标设备 ID (2认同)

nic*_*fox 6

罗技游戏鼠标G502我2美分

#!/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 
Run Code Online (Sandbox Code Playgroud)