I am parsing keyboard input via read -n 1 and I haven't found a way to distinguish between an actual key-press of the Escape key and the first Escape character (byte) of a control sequence generated by some other key-press, eg. LEFT, RIGHT...

I don't need the Escape key during the read, so disabling it should solve the problem. Is there some way to disable or re-assign just the Escape key in a localized manner? ...for just the current terminal, and for just the current script.

  • note: ncurses uses a timer to distinguish the two (see man 3x notimeout/timer), not sure if it is able to disable the escape key completely. – Stéphane Gimenez Aug 11 '11 at 21:57
  • Stephane: Thanks, It's useful to know that a timer is a tried-and-tested method.. – Peter.O Aug 12 '11 at 5:20

There is no way to do this from inside the terminal. Unfortunately, the same ESC character sees use both as the character sent by a particular key on the keyboard and as the prefix in multi-character sequences. The historical precedents on both sides are too strong to be overwhelmed.

Applications either avoid giving a meaning to Esc alone, or put a delay (1 second is common). For example, in vim, go into insert mode and press ESC O D very quickly; this is equivalent to pressing the Left key. But if you wait too long after pressing Esc, you'll switch to normal mode and insert a line above the current one (O normal mode command).

You might be able to reconfigure your terminal emulator so that the Esc key sends some escape sequence other than the ESC (\e) character. But that's likely to be backfire if you run an application that uses the ESC key intensively, such as vi(m).

  • Gilles, as you and Stéphane have both pointed out, the prevailing method appears to be to use a 'timer', so I've added a simple nanosecond timer... It works quite well, as the generated key sequences hit the script loop faster than I can type.. It may glytch occasionally due to system load, so I'm, including option for a speed check. Thanks... (that's quite interesting about vim.) – Peter.O Aug 12 '11 at 5:22

If you’re on a Mac, then I can heartily suggest the following pair of tools:

  • PCKeyboardHack, a utility to remap the caps lock key to anything else,
  • and KeyRemap4MacBook, another (more complex) utility by the same hacker to remap almost any other key (i.e. not caps lock)

Combined, I remap caps lock to Esc, and completely disable the actual Esc key (it’s so far “out there,” and using my remapped caps-lock key is a much better habit to be getting into.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.