The following code allows me to catch an escape key press. After evaluating the code, pressing ESC will print "Escape pressed", as desired.
Unfortunately, due to the equivalence between ESC and Meta, Meta keys do not function when this keymap is active. Instead, I see messages like
M-f is undefined
I would like to be able to evaluate the following block of code, press M-f, and have it invoke forward-word
as usual. Is there any way to achieve this?
(set-transient-map
`(keymap
(27 . ,(lambda () (interactive) (message "Escape pressed"))))
nil (lambda () (message "Other key pressed")))
(setq meta-prefix-char nil)
I evaluate it first thing when Emacs loads so keymaps defined subsequent thereto are aware of it. As an alternative approach, have a look at something Stefan wrote up for conditional use of the escape key to see if that interests you -- I used it for about a year or so -- the method is similar to how the universal argument works: stackoverflow.com/a/20036348/2112489(this-command-keys-vector)
, so I have accepted his solution. Sorry I can't give you partial credit for the pointer.