As an Emacs newbie (with two weeks of Emacs experience) I am in the process of understanding the way Emacs manages user input events.

My current understanding of how it works is that the way Emacs handles user input events seems to differ from what I have experienced yet for example with programming of Tkinter GUIs with Python.

Emacs seems not to have the ability to provide the information about an event which can be passed as an argument to a handler function. So there is no such thing as "onKeypress(event):" where inside of the function onKeypress() the event object can be evaluated in order to know which key/keys was/were pressed.

Emacs comes with a mechanism of 'hooks' where for each possible event a handler function can be added to a list of functions which will be executed when a specific event takes place.

In other words it gives an option to 'subscribe' to a specific user input event. Also a possibility to subscribe to a category of events is there, but without passing the information about the specifics of the event which triggered the subscribed function, so its usability is limited to applications where the specific case doesn't matter.

Is this above provided rough description the right one? Or is there something important I am missing in my way of understanding Emacs event management which will lead in future to confusion and weird questions?

  • Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on Emacs Meta, or in Emacs Chat. Comments continuing discussion may be removed.
    – Dan
    Commented Mar 28, 2023 at 10:00

1 Answer 1


How Emacs manages input events is documented at C-hig (elisp)Command Loop. See particularly "Input Event", "Reading Input", and "Special Events".

The basic command loop is: Wait for a key. (While we're waiting, handle output from any inferior processes.) Process a key when it arrives, using the hierarchy of keymaps. If the key is part of a prefix binding, wait for additional keys. If we have a complete key sequence bound to a command then run pre-command-hook, execute the command, and afterwards run post-command-hook. Then go back to the beginning.

(There's more to it if you dig into the details naturally, but this is the essence of what's going on.)

Everything you do interactively in Emacs is a command. If you type the A key on your keyboard and the letter A is inserted into the current buffer, that was due to the keymap lookup finding a key binding for A which was bound to a command for inserting that character. So there is no (quoting a comment) "catch user input in the background while keeping editing" -- the "editing" is nothing more than a series of executed commands based on input events, and any other user input is exactly the same thing. Emacs just loops over the input events, executing the commands they are bound to.

Almost everything which happens on account of user input is happening on account of keymaps. I recommend reading Mickey Petersen's excellent article on that subject: https://www.masteringemacs.org/article/mastering-key-bindings-emacs

If you want to read the C code, you'll find command_loop_1() and friends in the 13,000 line src/keyboard.c. It all gets kicked off by the Frecursive_edit() at the end of main() in emacs.c.

  • Can a key-press of a prefix key be noticed using an elisp function?
    – oOosys
    Commented Mar 27, 2023 at 3:22
  • "What if the prefix key is not followed by any key which makes to form a sequence for which there is a command?" -- I already feel like I'm wasting my time, and that sort of thing doesn't help. What happens?! You could find out for yourself by trying it in a matter of seconds! Good grief...
    – phils
    Commented Mar 27, 2023 at 3:48
  • And I don't know what you mean by "noticed". When do you want that to be "noticed"? For what purpose? read-key-sequence will be waiting for the remainder of the sequence and, while I might be wrong, I don't believe there's a way to interact with the read-key-sequence process, so it's up to the keymaps what happens after that.
    – phils
    Commented Mar 27, 2023 at 3:58
  • "What decides which mapping is to use?" -- I have updated the answer to link to an excellent article which you should read.
    – phils
    Commented Mar 27, 2023 at 4:00
  • The Emacs manual says that if a prefix key is not followed by an expected key nothing will be done. So I conclude from this that the 'wrong' key sequence is just 'forgotten' and the process of listening to user input events re-starts from the beginning. .
    – oOosys
    Commented Mar 27, 2023 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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