Detecting the presence/absence of a numeric keypad?

前端 未结 2 1341
死守一世寂寞
死守一世寂寞 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:41

    I don't believe you can query for keyboard capabilities but iirc there is a "numpad" key. ;)

    You should be able to query if Num is on or off, but it being off won't be a foolproof way to know that there is no numeric keypad.

    What you could probably do is detect keys being pressed on a numpad and subliminaly trick the user into doing so, which would identify said characteristic.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题