Detecting the presence/absence of a numeric keypad?

前端 未结 2 1340
死守一世寂寞
死守一世寂寞 2021-01-24 02:23

Is it possible to determine whether there is a numeric keypad connected to the system? Desktop keyboards typically have numpads, while laptops typically don\'t (though they put

2条回答
  •  盖世英雄少女心
    2021-01-24 02:47

    install these packages

    sudo apt-get install -y numlockx xdotool x11-utils
    

    then use a script like this

    #!/bin/bash
    cd
    numlockx off &
    rm -f s1.sh s2.sh out
     echo -e "#\041/bin/bash
    sleep 2
    xdotool key KP_5 | xev > out
    exit 0" >> s1.sh
    chmod +x s1.sh
    echo -e "#\041/bin/bash
    sleep 9
    killall -9 xev
    sleep 5
    xdotool key BackSpace
    exit 0" >> s2.sh
    chmod +x s2.sh
    
    
    bash s1.sh &
    bash s2.sh
    
    rm -f s1.sh s2.sh
    if [[ "$(cat out | grep "keycode 84" | grep "KP_Begin" )" != "" ]]; then
      echo "Separated Numpad Detected!"
    fi
    rm out
    exit 0
    

    because KP_Begin is the keycode for the numpad key KP_5 when numlock isn't active, in a real keyboard with a real keypad. Tested on 3 PCs, one with a separated keypad e 2 without it.

提交回复
热议问题