Using Vim Hex Editor To View Keyboard Key Hex Code
The best way to predict the future is to create it.
— Anonymous.
In this TIL (Today I Learned), we will review a way on how to view keyboard key hex code. As I modify my iTerm2 (a popular terminal emulator for macOS) key shortcuts to map my tmux Ctrl + b keys, I wonder how to get the keyboard key hex codes easily.
Then I remembered that there is the xxd
(a command line hex viewer and editor which is part of the vim package) command which can process keys and convert them to hex code.
To start off, we run xxd
from the terminal. It will wait for a read line. Execute your keystrokes (e.g. Ctrl – b) then press enter to create a new line. After the new line add EOF (End Of File) which would corresponds to the keyboard keys Ctrl + d. After doing the process above xxd
would output a hex representation of the keyboard key code that you desire.1
Another trick using xxd
command is to reverse hex string like this.
echo <hex code> | xxd -revert -plain | rev | tr -d '\n' | xxd -plain
An example hex code would be 030201
. That would output a reverse 010203
. The rev
command will reverse the output while the tr
would trim newline.