快速测试所有键盘按键的脚本

mdr*_*drg 14 grep bash keyboard awk stdout

我需要检查一些笔记本电脑的键盘按键是否有问题,所以我想尽可能地加快速度。

对于这个特定的任务,我没有找到任何东西,所以我的想法是一个脚本,它读取按下的键并知道所有的键盘键,所以我可以快速敲击它们并报告哪些尚未按下。我想我可以使用showkeyor来完成xev,grepping 输出:

xev | grep keysym
Run Code Online (Sandbox Code Playgroud)

示例输出:

state 0x10, keycode 46 (keysym 0x6c, l), same_screen YES,
state 0x10, keycode 33 (keysym 0x70, p), same_screen YES,
state 0x11, keycode 50 (keysym 0xffe1, Shift_L), same_screen YES,
state 0x10, keycode 51 (keysym 0x5d, bracketright), same_screen YES,
state 0x10, keycode 36 (keysym 0xff0d, Return), same_screen YES,
Run Code Online (Sandbox Code Playgroud)

可读的键符非常有用,但我一直在测试键码,因为它们不会随着修饰键的打开/关闭(大写锁定、数字锁定)而改变。我是 bash 新手,所以我正在整理一些东西。这是迄今为止最好的结果:

#!/bin/bash

function findInArray() {
    local n=$#
    local value=${!n}
    for ((i=1;i < $#;i++)) {
    if [[ ${!i} == ${value}* ]]; then
    echo "${!i}"
    return 0
    fi
    }
    echo
    return 1
}

list=( 38:a 56:b 54:c 40:d 26:e 36:Return 50:Shift_L )
xev | \
# old grep solution
# grep -Po '(?<=keycode )[0-9]+(?= \(keysym 0x)' | \
# 200_success' suggestion
awk '/state 0x.*, keycode / { print $4; fflush() }' | \
while read keycode ; 
do
  found=$(findInArray "${list[@]}" ${keycode})
  if [[ $found ]]; then
    echo Pressed $found
    list=(${list[@]/${keycode}\:*/})
    echo 'Remaining ===>' ${list[@]}
    if [[ ${#list[@]} == 0 ]]; then
      echo All keys successfully tested!
      pkill xev
      exit 0
    fi
  fi
done
Run Code Online (Sandbox Code Playgroud)

虽然我使用grep它只是在我关闭时打印输出xev,但它最终也不会杀死它。awk@200_success的建议解决了这些问题,但它不会立即打印输出:需要 5-6 次击键才能“刷新”输出。我该如何解决?

注意:我知道这个脚本对于每个不同型号的键盘都需要不同的键列表,但这没关系,因为我只有几个型号要测试。


编辑 1:我用最新的脚本代码编辑了问题。

编辑 2:根据@200_success 建议更新脚本。

200*_*ess 5

尝试用刷新其输出grepawk脚本替换您的行。

xev | \
awk '/state 0x.*, keycode / { print $4; fflush() }' | \
while read keycode ; do
    # etc.
done
Run Code Online (Sandbox Code Playgroud)