2

Q: how do I get this-command-key-vector to return a character vector when called with the return key?

Consider the following simple command, which will echo the vector of keys that invoked it:

(defun test-echo ()
  (interactive)
  (message "%s" (this-command-keys-vector)))

Now let's bind it to two keys:

(local-set-key "i" #'test-echo)
(local-set-key [return] #'test-echo)

When typing i, the function correctly messages me with the vector [105], which corresponds to i's character representation (eq ?i 105). However, when typing return, it echoes [return], when I would have expected [10], i.e., ?\n.

First of all, is this a problem with my understanding of this-command-key-vector, my understanding of return, or both? Second of all, how do I get this-command-key-vector to return a character (ie, the integer value) rather than the symbol return (or, alternately, how do I translate return to a character)?

2

It's a problem with your understanding of return.

<return> is a pseudo-function key. It is not the same thing as RET, which corresponds to C-m (decimal 13). Depending on your keyboard, when you hit the Enter or Return key, Emacs might receive one or the other. Use C-h k followed by hitting that key, to see what Emacs tells you it receives. For example, when I do that I see this:

RET (translated from <return>) runs the command...

[return] is the vector representation of the pseudo-function key <return>.

| improve this answer | |
  • Interesting! Would it therefore be accurate to say that it's not sensible to translate [return] to a character integer in the same way that it's not sensible for, say, [f10]? – Dan Apr 6 '15 at 18:28
  • @Dan: Absolutely. At least that's my understanding. (I'm no expert on this.) – Drew Apr 6 '15 at 20:57

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.