last day (14 days later) » 

3:49 AM
3
A: How can I simulate an arbitary key event from Elisp?

resuemanTaken from this answer, you can use global-set-key like this (global-set-key (kbd "C-`") (kbd "<escape>")) Which will treat C-` as escape This does seem to have some problems though if the second combination doesn't execute a function. So if escape is being used like Meta, then it doesn't wor...

 
This is absolutely NOT what I am looking for. Thanks for the answer, but I want to simulate a key event from elisp not map one binding to another. The example you have provided does not work. Executing that code will not simulate pressing escape in any context at all. The only reason that I am being so emphatic about this is that this answer has already received so many upvotes that I'm worried no one else will bother to answer the question.
 
@nispio: Actually, it does work, since the second argument is implicitly converted to a keyboard macro.
 
@shosti Evaluating the above and then pressing C-` gives me an error: After 0 kbd macro iterations: command-execute: Lisp nesting exceeds `max-lisp-eval-depth'.
 
@nispio: You probably already have C- ` bound to ESC by some other method, so it's going into an infinite loop.
 
@shosti You were right. Too many eval-sexp going on in one session. :-) But trying again with emacs -Q causes C-` to simply do nothing.
 
3:49 AM
Depending on your system, (kbd "<escape>") and (kbd "ESC") might mean different things--have you tried both?
 
ESC does nothing, <escape> says After 0 kbd macro iterations: command-execute: Lisp nesting exceeds `max-lisp-eval-depth'.
All of that is with emacs -Q
 
4:35 AM
hmm, strange. "ESC" also doesn't work for me, but "<escape>" works as expected (on OS X with Cocoa). You're sure you didn't evaluate anything else first?
 
I start fresh with emacs -Q and then I run M-: (global-set-key (kbd "C-`") (kbd "<escape>")). After that, I press C-` and get the error about max-lisp-eval-depth.
GNU Emacs 24.4.1 (x86_64-unknown-linux-gnu, GTK+ Version 2.20.1)
What do you get? Are you able to run C-` x to get M-x?
Inline code doesn't stand out very well here in chat.
 
Oh, I see now, it also happens to me to in Emacs -Q (I had ESC bound to something else). I guess ESC is treated specially because of its use as the Meta prefix, but I don't really know the details (might be worth its own question). In general, though, the global-set-key trick (or execute-kbd-macro) works pretty well.
 
4:52 AM
I think that in general this only works for key sequences that have a binding. You can replace 'ESC' with just about any other prefix (e.g. 'C-x' or 'C-h') and this will not do what you want.
It is as if the macro exits, and aborts whatever key sequence was in process. (Just guessing)
 
Yeah, I think you're right, it doesn't work with prefixes--(kbd "C-h k") barfs but (kbd "C-h k k") works as expected.
But then again, the call-interactively solution has exactly the same problem.
 
Yes, 'call-interactively' is not a good solution either. Most of the presented solutions depend on the key sequence having a binding.
 
I think key-translation-map is probably the most general solution if you also want prefixes.
 
If you are looking at mapping a single keystroke to some other keystroke or command, then yes. It does nothing for mapping a longer key sequence (e.g. C-x r <f7>) to another keystroke or event, and it also cannot be called from elisp, which is the subject of the question.
However, after spending some time looking at the various solutions, I think that in most cases there is a better way of achieving a desired result than to simulate a key event from elisp code.
It just won't be a one-size-fits-all solution, which is more like I was dreaming of. You would have to figure out on a case-by-case basis whether translation keymaps, overlay keymaps, or keyboard macros is the best tool for the job.
 

last day (14 days later) »