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](https://www.google.com/search?q=linux+see+what+key+is+being+pressed&oq=linux+see+what+key+is+being+pressed&aqs=chrome..69i57j69i59.198j0j9&sourceid=chrome&ie=UTF-8), 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
1. [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
1. [this very question here](https://unix.stackexchange.com/questions/120199/how-to-detect-global-key-presses)
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
1. 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`
```bash
# 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
1. `sudo showkey`
```bash
# 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
1. 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
1. `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.
1. Click back into your terminal window and press <kbd>Ctrl</kbd> + <kbd>C</kbd>.
Sources:
1. Where I first learned about this tool: https://askubuntu.com/a/1198027/327339
1. See also: https://unix.stackexchange.com/questions/120199/how-to-detect-global-key-presses/120201#120201
1. `sudo evtest` - a very low-level tool; appears to be what I need to work on USB HID devices (I think); works great!
```bash
# 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.
END