Ubuntu: show what keys are pressed in real-time [duplicate]
I am looking for a tool that tells what keys (including Alt, Shift, Ctrl, etc) are pressed now. Need it to do a health-check on a possibly faulty keyboard.
Install keymon
. It's in the Universe repository and run it using key-mon
(not keymon
!).
man keymon has this:
Keymon - Keyboard and mouse monitor window for GTK.
Do read man keymon
for all the options available.
You should also right-click on it to check that the settings are appropriate for you.
And if you don't like the default location, drag it to a more suitable position on your screen.
An alternative is screenkey
, also in the Universe repository. A feature of screenkey is that it's interface only when you type something and disappears after a few seconds if the keyboard is inactive. However, unlike keymon, screenkey doesn't register mouse clicks.
There's a YouTube video on both keymon
and screenkey
. The video's in German but still is easy to follow.
To test a possibly faulty keyboard it's best to go as low-level as possible. One of the easiest ways to do this without diving into kernel space is to work almost directly with /dev/input/event*
device files. Namely, you can use evtest
to see all the keyboard input. If you run it in grabbing mode, this will let you intercept everything—even Magic SysRq combos (funnily, even SAK)!
Here's how I'd go about it. First, get a list of input devices by running sudo evtest
:
$ sudo evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0: Power Button
/dev/input/event1: Power Button
/dev/input/event2: PC Speaker
/dev/input/event3: Video Bus
/dev/input/event4: HDA Intel HDMI HDMI/DP,pcm=3
/dev/input/event5: HDA Intel HDMI HDMI/DP,pcm=7
/dev/input/event6: HDA Intel HDMI HDMI/DP,pcm=8
/dev/input/event7: HDA Intel HDMI HDMI/DP,pcm=9
/dev/input/event8: HDA Intel HDMI HDMI/DP,pcm=10
/dev/input/event9: HDA Intel PCH Front Mic
/dev/input/event10: HDA Intel PCH Rear Mic
/dev/input/event11: HDA Intel PCH Line
/dev/input/event12: HDA Intel PCH Line Out
/dev/input/event13: HDA Intel PCH Front Headphone
/dev/input/event14: HDA NVidia HDMI/DP,pcm=3
/dev/input/event15: HDA NVidia HDMI/DP,pcm=7
/dev/input/event16: HDA NVidia HDMI/DP,pcm=8
/dev/input/event17: ImExPS/2 Generic Explorer Mouse
/dev/input/event18: AT Translated Set 2 keyboard
Select the device event number [0-18]:
Don't choose anything here yet: just press Ctrl+C. This run of evtest
was in a simple non-grabbing mode, which won't let you intercept everything. But you now know the device file you need (in my case given above, it's /dev/input/event18
).
Now you can actually run evtest
in grabbing mode, using the --grab
option, so that it intercepts all events from the keyboard—including the release of Return after you submit your command to the shell, subsequent Ctrl+C, Magic SysRq, VT switch shortcuts etc.. To avoid being locked out of the system, we'll set up a timeout for evtest
.
sudo su -c 'sleep 1; timeout -k5 10 evtest --grab /dev/input/event18'
This command does the following:
- Waits 1 second so that you can release Return before the keyboard is grabbed (otherwise you'll get autorepeats rapidly scrolling the console)
- Starts
evtest
in grabbing mode on my keyboard's device file (replace it with yours). -
evtest
is run with a timeout of 10 seconds, and additional grace period of 5 seconds in (unlikely) case it hangs, after which it's killed bySIGKILL
, hopefully returning keyboard control to you. -
sudo
is wrapped around the whole command instead of onlyevtest
to make sure that you enter the password (if needed) beforesleep 1
, otherwise this sleep will be useless
During the timeout of 10 seconds (which you can, of course, change to something that suits you better) you can press anything on your keyboard—aside maybe from Fn-driven keys, which might work in a nonstandard way—and see what it inputs.
There is a website https://www.keyboardtester.com/ which lets you see which keys you have pressed, and it shows the keyboard layout. Also, there is a package called xkeycaps
which can be used. Moving the mouse over a key describes the keysyms and modifiers that that key generates
There are more sites available like
- http://en.key-test.ru/
- http://keyboardchecker.com/
- https://keyboardtester.co/keyboard-tester.html
- https://www.onlinemictest.com/keyboard-test/
xev
is also an option. Just run:
xev
Make sure that the small, white window that opens is selected and press any key to see it.
To limit xev
's rather verbose output, so that it only shows the keys that you press, you can pass its output to awk
:
xev | awk /keysym/'{sub(/\)\,/,"", $7); print $7}'
In any case, note that xev
registers two events for every key that you press, one for pressing the key and one for releasing it.