223

In Windows there is a program called Keyboard Jedi that shows what keys are pressed on the keyboard.

Is there an equivalent for Linux?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
ftravers
  • 2,604
  • 3
  • 16
  • 11
  • 2
    I was looking for something that helps with screen casts...screenkey seem like what I wanted. – ftravers Nov 15 '15 at 18:21
  • Xev os great option and probably is already installed onyour machine – deFreitas Nov 02 '18 at 13:30
  • 1
    I am happy to find this question. It should be reopened as [on-topic] because it is about "computer software" - similar as [software-to-show-typed-keys-on-the-screen](https://superuser.com/questions/311199/software-to-show-typed-keys-on-the-screen/311221#311221). – hc_dev Dec 08 '21 at 07:31
  • See also [Ubuntu: show what keys are pressed in real-time](https://askubuntu.com/questions/1197651/ubuntu-show-what-keys-are-pressed-in-real-time). I find the https://www.keyboardtester.com/ and `evtest` answers there to be very useful, and both of them work. – Gabriel Staples Dec 20 '21 at 16:56
  • Since this question is closed, this is probably the best place to add any new answers that may come up: [Unix & Linux: How to detect global key presses](https://unix.stackexchange.com/questions/120199/how-to-detect-global-key-presses) – Gabriel Staples Dec 20 '21 at 17:16

11 Answers11

199

Others have mentioned xev, which is good when you're running X11. When you're at the console, however, showkey is what you want.

And if you’re at an SSH session or a real terminal, you can use /usr/lib/ncurses/examples/demo_altkeys (available in Debian in the ncurses-examples package).

Note that xev for "a" gives 38, while showkey for "a" gives 30, because xev reports scancodes, whereas showkey by default reports keycodes.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Teddy
  • 6,848
  • 4
  • 19
  • 19
  • 5
    Neither work in SSH terminal. – anatoly techtonik Nov 08 '15 at 11:23
  • @anatolytechtonik: Guess you gotta get a new Innernet :( – David Rivers May 24 '16 at 17:50
  • @anatolytechtonik For me, xev works fine via ssh. Did you add -X or -Y to the ssh options on the ssh command line? – Christian Hujer Jan 10 '17 at 10:24
  • 4
    @ChristianHujer, -X -Y means X11 forwarding (https://stackoverflow.com/questions/13425820/difference-between-ssh-x-and-ssh-y). There is no X11 server on my machine (I use PuTTY), so `xev` doesn't apply, and `showkey` gives `Couldn't get a file descriptor referring to the console`. – anatoly techtonik Jan 10 '17 at 12:15
  • @anatolytechtonik Yes, if you use PuTTY, or any environment where X11 forwarding doesn't work, it will not work. Your statement just sounded like it would never work in a terminal, and that's simply not true. `xev` will work in `ssh` in a terminal if X11 forwarding is enabled and functional. – Christian Hujer Jan 10 '17 at 16:52
  • 23
    to work around `showkey` giving the error about not getting a file descriptor (like in comment by @anatolytechtonik ), try `sudo showkey` – pestophagous Mar 27 '17 at 22:38
  • Why do these two approaches give different results? Example: `xev` for "a" gives 38, while `showkey` for "a" gives 30. – Johann Aug 10 '17 at 18:19
  • 9
    @Johann `xev` reports scancodes whereas `showkey` by default reports keycodes. `sudo showkey -s` reports scancodes. – Nizam Mohamed Oct 17 '17 at 09:01
  • `locate demo_altkeys` might point to the matching binary in any UNIX system, if it's there and that the file database is up-to-date (`updatedb` if you have the relevant privileges) – psychoslave Nov 30 '18 at 08:57
  • Use `libinput debug-events` which is also "Wayland" compatible, see [man(1)](https://www.mankier.com/1/libinput-debug-events). – goetz Aug 15 '19 at 23:54
  • But it can not show timing information. [xev](https://superuser.com/questions/248517/show-keys-pressed-in-linux/248518#248518) does. – Peter Mortensen Jan 15 '20 at 08:42
  • Is there a way to prevent `showkey` from timing out after 10 seconds without doing ASCII dump mode? – Daniel Giger May 15 '20 at 03:09
  • 5
    Only `showkey -a` works over an SSH PTY or a serial terminal (`/dev/ttyS`_n_). It's sensible, as the scancode/keycode/emission byte sequence is handled on the client end, in the TTY emulator (or hardware, if you're a fan of Retrocomputing.SO). You put these sequences into `.inputrc` for e.g. bash, and other readline-happy stuff. The remote X stuff works differently: [e.g., Windows-based] X server has access to your raw keyboard (Windows reserves some chords, tho), and sends scancodes. Over `ssh -Y` or TCP, all the same. There's also the terminfo DB (demo_altkeys), but this margin is too narr.. – kkm inactive - support strike Jun 06 '21 at 20:22
  • Summary: `showkey` doesn't work; `sudo showkey` works great and shows numeric keycodes, `sudo showkey -s` shows hex scancodes, and `sudo showkey -a` shows ASCII keycodes, including the actual letter or character pressed, which is the most human-readable. Note: to exit the `sudo showkey -a` option, use Ctrl + D. Ctrl + C works in most of the other tests, but NOT in that one. – Gabriel Staples Dec 20 '21 at 17:02
  • I added my summary above to [my answer here](https://unix.stackexchange.com/a/683257/114401). Unfortunately, this question is closed or I'd have added it here. – Gabriel Staples Dec 20 '21 at 22:37
85

Also screenkey, a screencast tool that displays keys being pressed on-screen.

Adobe
  • 2,649
  • 2
  • 28
  • 34
42

There is the xev program for graphic mode - see the man page of xev.

On Ubuntu/Debian it is packed into the x11-utils package.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
osgx
  • 6,677
  • 8
  • 56
  • 71
42
 chris@retina:~$ xinput list
 ⎡ Virtual core pointer                     id=2    [master pointer  (3)]
 ⎜   ↳ Virtual core XTEST pointer               id=4    [slave  pointer  (2)]
 ⎜   ↳ bcm5974                                  id=13   [slave  pointer  (2)]
 ⎜   ↳ Logitech Unifying Device. Wireless PID:1028  id=9    [slave  pointer  (2)]
 ⎣ Virtual core keyboard                    id=3    [master keyboard (2)]
     ↳ Virtual core XTEST keyboard              id=5    [slave  keyboard (3)]
     ↳ Power Button                             id=6    [slave  keyboard (3)]
     ↳ Power Button                             id=7    [slave  keyboard (3)]
     ↳ Sleep Button                             id=8    [slave  keyboard (3)]
     ↳ FaceTime HD Camera (Built-in)            id=11   [slave  keyboard (3)]
     ↳ Apple Inc. Apple Internal Keyboard / Trackpad    id=12   [slave  keyboard (3)]
     ↳ daskeyboard                              id=10   [slave  keyboard (3)]
     ↳ daskeyboard                              id=14   [slave  keyboard (3)]
 chris@retina:~$ xinput test 14
 key release 36 
 key press   43 
 hkey release 43 
 key press   26 
 ekey release 26 
 key press   46 
 lkey release 46 
 key press   46 
 lkey release 46 
 key press   32 
 okey release 32 
 key press   37 
 key press   54 
 ^C
 chris@retina:~$ 
Christopher Done
  • 770
  • 7
  • 11
  • 2
    xev failed to show me Mx Anywhere 2 mouse buttons, this method displayed correct button. –  Jul 09 '17 at 13:43
40

If you are looking for something that graphically shows you what key is pressed currently (perhaps for the corner of a screencast), key-mon might be the ticket.

asjo
  • 539
  • 5
  • 5
  • If you can find the increasingly difficult-to-find Python 2.x and PyGTK (GTK+ 2.x) dependencies, I'd recommend key-mon as the nicest-looking option for screencasts. (I'd try my hand at porting to Python 3.x and GTK+ 3.x, but I have too much else on my plate that's more urgently needed.) – ssokolow Jan 25 '20 at 08:43
  • key-mon is pretty cumbersome to install unfortunately, code hasn't been updated/maintained in over 8 years and dependencies aren't easy to find as @ssokolow mentioned. The only alternative is screenkey which is also a bit weird but does work. – SidOfc May 21 '20 at 13:42
40

The better command that I know for this functionality is showkey, with parameter -a.

You could try, and Ctrl + D to exit:

showkey -a
Glorfindel
  • 4,089
  • 8
  • 24
  • 37
23

You can also use evtest. In some situations, it is better than xev as it shows keys even when a key is already captured.

To install under Ubuntu/Linux Mint, do

sudo apt-get install evtest

to run sudo evtest and pick a device number.

Example output:

$ sudo evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0:    Lid Switch
/dev/input/event1:    Power Button
/dev/input/event2:    Power Button
/dev/input/event3:    AT Translated Set 2 keyboard
/dev/input/event4:    ETPS/2 Elantech Touchpad
/dev/input/event5:    Logitech Logitech G930 Headset
/dev/input/event6:    Video Bus
/dev/input/event7:    HDA Intel HDMI HDMI/DP,pcm=8
/dev/input/event8:    HDA Intel HDMI HDMI/DP,pcm=7
/dev/input/event9:    HDA Intel HDMI HDMI/DP,pcm=3
/dev/input/event10:    HDA Intel PCH Headphone
/dev/input/event11:    HDA Intel PCH Mic
/dev/input/event12:    WebCam SC-10HDP12B24N
/dev/input/event13:    ELAN Touchscreen
Select the device event number [0-13]: 5
Input driver version is 1.0.1
Input device ID: bus 0x3 vendor 0x46d product 0xa1f version 0x101
Input device name: "Logitech Logitech G930 Headset"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 114 (KEY_VOLUMEDOWN)
    Event code 115 (KEY_VOLUMEUP)
    Event code 163 (KEY_NEXTSONG)
    Event code 164 (KEY_PLAYPAUSE)
    Event code 165 (KEY_PREVIOUSSONG)
    Event code 256 (BTN_0)
    Event code 257 (BTN_1)
    Event code 258 (BTN_2)
    Event code 259 (BTN_3)
    Event code 260 (BTN_4)
    Event code 261 (BTN_5)
    Event code 262 (BTN_6)
    Event code 263 (BTN_7)
    Event code 264 (BTN_8)
    Event code 265 (BTN_9)
  Event type 4 (EV_MSC)
    Event code 4 (MSC_SCAN)
Properties:
Testing ... (interrupt to exit)
Event: time 1412585327.807585, type 4 (EV_MSC), code 4 (MSC_SCAN), value c00b5
Event: time 1412585327.807585, type 1 (EV_KEY), code 163 (KEY_NEXTSONG), value 1
Event: time 1412585327.807585, -------------- SYN_REPORT ------------
Event: time 1412585327.927557, type 4 (EV_MSC), code 4 (MSC_SCAN), value c00b5
Event: time 1412585327.927557, type 1 (EV_KEY), code 163 (KEY_NEXTSONG), value 0
Event: time 1412585327.927557, -------------- SYN_REPORT ------------
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Kamil Dziedzic
  • 328
  • 3
  • 5
13

If you're at a shell prompt, you can press Ctrl-v then the key of interest to see what the output is. For example, on my system, pressing Ctrl-v then Right Arrow shows ^[[C which means Escape, Left Bracket, C.

As others have mentioned, xev is the way to go for X11.

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
7
xev | grep 'keycode'
    state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES,
    state 0x0, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    state 0x4, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    state 0x0, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    state 0x4, keycode 52 (keysym 0x7a, z), same_screen YES,
    state 0x4, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    state 0x0, keycode 54 (keysym 0x63, c), same_screen YES,
    state 0x0, keycode 52 (keysym 0x7a, z), same_screen YES,
    state 0x0, keycode 54 (keysym 0x63, c), same_screen YES,
LeCanardNoir
  • 89
  • 1
  • 1
7

You'd be wanting xev.

Tony Miller
  • 221
  • 1
  • 3
1

If you only want to see a human-friendly-ish output of the key pressed, you can use this:

xev | sed -n 's/[ ]*state.* \([^ ]*\)).*/\1/p'

Note you'll get the keypress output on both keydown and keyup.

ACK_stoverflow
  • 768
  • 1
  • 8
  • 20