如何根据 /proc/bus/input/devices 的输出获取实际的键盘设备?

如何根据 /proc/bus/input/devices 的输出获取实际的键盘设备?

当我跑步时:

cat /proc/bus/input/devices

...我收到以下输出:

...

I: Bus=0003 Vendor=040b Product=2000 Version=0110
N: Name="Generic USB Keyboard"
P: Phys=usb-0000:00:1d.0-1/input0
S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb5/5-1/5-1:1.0/input/input14
U: Uniq=
H: Handlers=sysrq kbd event7 
B: PROP=0
B: EV=120013
B: KEY=1000000000007 ff9f207ac14057ff febeffdfffefffff fffffffffffffffe
B: MSC=10
B: LED=7

...

有什么方法可以让脚本解析此信息并确定/dev/input/...代表此键盘的相应设备(在 中)?

如果有帮助的话,我打算使用设备名称xorg.conf

答案1

在你的输出中看到:

H: Handlers=[...] event7

在哪里事件7是设备:

/dev/input/event7

答案2

我只是被迫为此目的编写一个 bash 解析器。这是来源:

#!/bin/sh

inputDevicesInfoFilePath ()
{
    echo "/proc/bus/input/devices"
}


inputDevicesInfo ()
{
    cat $(inputDevicesInfoFilePath)
}

# arguments: device name, file line prefix
inputDeviceValue ()
{
    # constants
    local INFO_FILE=$(inputDevicesInfoFilePath)
    local NAME_PREFIX="N: Name="

    # name the function arguments
    local devName=$1
    local linePrefix=$2 

    # find the line number in the info file containing 
    # both the name prefix and device name argument 
    local lnNo=$(grep -n "${NAME_PREFIX}" ${INFO_FILE} | grep ${devName} | head -n1 | cut -d: -f1)

    # starting from the line number previously determined,
    # find the first line which contains the prefix argument
    # and extract the value token from that line
    local value=$(tail +${lnNo} ${INFO_FILE} | grep "${linePrefix}" | head -n1 | cut -d= -f2)

    # "return" the value via an echo  
    # if no value was found, don't echo anything 
    # but (literally) return an error code 
    if [ -z "${value}" ] ; then return 1; fi;
    echo ${value}
}

# arguments: device name
inputDevicePhys ()
{
    echo $(inputDeviceValue $1 "P: Phys=")
}

# arguments: device name
inputDeviceSysfs ()
{
    echo $(inputDeviceValue $1 "S: Sysfs=")
}

# arguments: device name
inputDeviceHandlers ()
{
    echo $(inputDeviceValue $1 "H: Handlers=")
}

# arguments: device name
inputDeviceEventHandlerPath ()
{
   # constants
    local INPUT_DEVICE_DIR_PATH="/dev/input/"

    # get the handlers for the device (as a space delimited list) 
    # if nothing is found return error code 1 and don't echo anything
    local handlers=$(inputDeviceHandlers $1)
    if [ -z "${handlers}" ] ; then return 1; fi;

    # interate through the list (splits on white space implictly)
    for handler in ${handlers}
    do
        # if the handler starts with "event", then echo the path 
        # and return from the function successfully 
        case ${handler} in event*)
            echo ${INPUT_DEVICE_DIR_PATH}${handler}  
            return
        esac
    done

    # if no event handler was found, don't echo anything 
    # but (literally) return an error code 
    return 1
}

我将其制作成一个名为 的独立脚本input-device-info.sh,并将其放入/usr/bin目录中(这可能是也可能不是“正确”的位置,但在我的用例中似乎有意义)。

然后,我从另一个客户端实现脚本中获取该脚本并调用该inputDeviceEventHandlerPath函数,如下所示:

. /usr/bin/input-device-info.sh   

MY_DEVICE_PATH=$(inputDeviceEventHandlerPath "my-device-name")
echo 'MY_DEVICE_PATH:' ${MY_DEVICE_PATH}

答案3

如果您想要一个较小的代码片段作为相邻答案中 @BuvinJ 代码的替代:

#! /usr/bin/env bash

# Somewhat stolen from: <https://unix.stackexchange.com/a/507209>
# Problem: multiple devices with the same name
find_event() {
    local lnNo handlers
    local INFO_FILE='/proc/bus/input/devices'
    lnNo=$(grep -n 'N: Name=' $INFO_FILE | grep "$1" | head -n1 | cut -d: -f1)
    handlers="$(tail +"$lnNo" $INFO_FILE | grep 'H: Handlers=' | head -n1 | cut -d= -f2)"

    [ -z "$handlers" ] && return 1

    for handler in $handlers; do
        case $handler in event*)
            echo "/dev/input/$handler"
            return 0
        esac
    done

    return 1
}

相关内容