Skip to main content
2 of 2
added 181 characters in body
Gabriel Staples
  • 3.1k
  • 3
  • 34
  • 52

I'd like to post a summary of the various tools I've just tested.

I began with this google search: linux see what key is being pressed, which brought me to the following places. Since they are either closed or off-topic, right here is the most-logical choice to post my summary.

  1. [closed: off-topic] https://superuser.com/questions/248517/show-keys-pressed-in-linux
  2. [closed: duplicate] https://askubuntu.com/questions/1197651/ubuntu-show-what-keys-are-pressed-in-real-time
    1. [the "original", non-duplicate question] https://askubuntu.com/questions/30466/how-can-i-display-keyboard-shortcuts-as-i-press-them
  3. this very question here

Here is a bit of a summary of the best answers from each of those places. I need this info. so I can:

  1. detect stuck keys, which just happened, and
  2. work on HID keyboard microcontroller devices, which I do occasionally since I sell one.

Summary of various ways to detect key presses

Tested on Linux Ubuntu 20.04.

  1. screenkey

    # Install it sudo apt update sudo apt install screenkey # run it screenkey # it now displays a massive black bar at the bottom of your main monitor # whenever you press any key, showing all keys as they are pressed! # kill it # first, find its PID ps aux | grep screenkey # Sample output: # $ ps aux | grep screenkey # gabriel 215523 2.9 0.3 972900 59364 ? Rl 10:27 0:00 /usr/bin/python3 /usr/bin/screenkey # gabriel 215635 0.0 0.0 9040 2388 pts/0 S+ 10:27 0:00 grep --color=auto screenkey # OR pgrep screenkey # Sample output: # $ pgrep screenkey # 215523 # now, kill that PID; ex: kill 215523 

    Where I first learned about this tool: https://askubuntu.com/a/30468/327339

  2. sudo showkey

    # show numeric keycode key presses and releases (use Ctrl + C to exit) sudo showkey # show hex scancodes (use Ctrl + C to exit) sudo showkey -s # show ASCII keycodes, including the actual letter or character pressed, # which is the most human-readable (use `Ctrl + D` to exit, NOT `Ctrl + C`!) sudo showkey -a 

    Source:

    1. https://superuser.com/a/248568/425838
      1. My summary comment under the answer: https://superuser.com/questions/248517/show-keys-pressed-in-linux#comment2606625_248568
  3. https://www.keyboardtester.com/. Test your keys in the browser, and/or see if a key is stuck. Where I first learned about this tool: https://askubuntu.com/a/1197656/327339

  4. xev. When done using it, it takes two steps to kill it:

    1. Click out of your current terminal window to cause your terminal window to lose focus.
    2. Click back into your terminal window and press Ctrl + C.

    Sources:

    1. Where I first learned about this tool: https://askubuntu.com/a/1198027/327339
    2. See also: How to detect global key presses
  5. sudo evtest - a very low-level tool; appears to be what I need to work on USB HID devices (I think); works great!

    # Install it sudo apt update sudo apt install evtest # Run it # First, find your keyboard /dev/input/event number manually sudo evtest # Then, press Ctrl + C to kill it after it prints out all the events; you # do NOT need to "Select the device event number" as it requests. Just hit # Ctrl + C. # Let's assume ours is "/dev/input/event4". # now, run the grab command with the above event number path sudo su -c 'sleep 1; timeout -k5 10 evtest --grab /dev/input/event4' # OR, if you know that `sudo evtest` only shows one output line with the # word "keyboard" in it, you can script the above two steps with this one # cmd like this. Notice that you can get a list of all input devices with # `cat /proc/bus/input/devices`, as is done at the start of this cmd: input_event_num="$(cat /proc/bus/input/devices \ | grep -B 1 -A 10 -i "keyboard" | awk 'NR==6 {print $4}')"; \ path_to_keyboard="/dev/input/$input_event_num"; \ sudo su -c "sleep 1; timeout -k5 10 evtest --grab $path_to_keyboard" 

    Source:

    1. https://askubuntu.com/a/1197742/327339
      1. See also the comments under this answer.

Other references:

  1. awk help: https://unix.stackexchange.com/a/89641/114401
  2. Where I learned about cat /proc/bus/input/devices: https://stackoverflow.com/a/15052092/4561887
Gabriel Staples
  • 3.1k
  • 3
  • 34
  • 52