This article is from the 3b1 computers FAQ, by John B. Bunch with numerous contributions by others.
Some programs that use the TAM (Terminal Access Method) library can leave the keyboard (really the shell window) in a strange state after leaving. This state is characterized by each press of the ESC key injecting two 0x1b characters into the input stream. This generally doesn't help anybody -- vi beeps too much, emacs is unusable, etc. The fix is to issue the following ioctl from a C program:
#include <sys/window.h> ioctl(0, WIOCSESC, 0); /* to turn off double-esc mode */
If you really want it back again, do the following:
ioctl(0, WIOCSESC, 1); /* to turn on double-esc mode */
 
Continue to: