Skip to content

Instantly share code, notes, and snippets.

@jtyr
Last active December 4, 2022 11:43
Show Gist options
  • Save jtyr/a1257168f0d19cfa7a573f754b6cdf85 to your computer and use it in GitHub Desktop.
Save jtyr/a1257168f0d19cfa7a573f754b6cdf85 to your computer and use it in GitHub Desktop.
Capture keyboard and mouse events on Linux

Instructions how to capture keyboard and mouse events and turn them into human readable output.

  1. Run evtest without any argments and see which event device you need to use.
  2. Run the following command for the keyboard events:
evtest /dev/input/event5 | grep --line-buffered 'EV_KEY' | sed -r -e 's/.*\(KEY_/Keyboard: /' -e 's/\), value 0/ - UP/' -e 's/\), value 1/ - DOWN/' -e 's/\), value 2/ - HOLD/'

Example of the output:

Keyboard: Z - DOWN
Keyboard: Z - UP
Keyboard: SPACE - DOWN
Keyboard: SPACE - HOLD
Keyboard: SPACE - HOLD
Keyboard: SPACE - HOLD
Keyboard: SPACE - UP
Keyboard: LEFTCTRL - DOWN
Keyboard: LEFTCTRL - HOLD
Keyboard: LEFTCTRL - HOLD
Keyboard: LEFTCTRL - HOLD
Keyboard: LEFTCTRL - UP
  1. Run the following command for the mouse events:
evtest /dev/input/event7 | egrep --line-buffered '(EV_KEY|REL_WHEEL_HI_RES)' | sed -r -e 's/.*\((BTN_|REL_WHEEL_HI_RES)/Mouse: /' -e 's/\), value -120/WHEEL - DOWN/' -e 's/\), value 120/WHEEL - UP/' -e 's/\), value 0/ - UP/' -e 's/\), value 1/ - DOWN/' -e 's/\), value 2/ - HOLD/'  

Example of the output:

Mouse: WHEEL - UP
Mouse: WHEEL - UP
Mouse: WHEEL - UP
Mouse: LEFT - DOWN
Mouse: LEFT - UP
Mouse: MIDDLE - DOWN
Mouse: MIDDLE - UP
Mouse: RIGHT - DOWN
Mouse: RIGHT - UP
Mouse: WHEEL - DOWN
Mouse: WHEEL - DOWN
Mouse: WHEEL - DOWN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment