Inverse Function (kbd)?

I was wondering if there is an inverse function (kbd) that I could use to get a meaningful description from the key code, for example Cb from 2 .

Normally (format "%c" x) works, but not for the previous example, as well as for many others, since key bindings, especially to Emacs, are often more complicated than a single character, and even when they really are a single character, they can be space or tab.

It seems that the documentation functions use the (describe-buffer-bindings) function, which is written in C and therefore immutable, and I would like to avoid this route, if possible, and manipulate the data in my path to provide more interesting functions.

+7
source share
2 answers

You are probably looking for (key-description KEYS &optional PREFIX) .

For example: (key-description [2]) and (key-description (kbd "Cb")) are evaluated to "Cb" .

+10
source

Inverse kbd definition

 (defun my-kbd-inverse (key) (if (numberp key) (setq key (vector key))) (key-description key)) 

Test

 (defvar my-example-keys nil) (setq my-example-keys (list 2 3 ?a 24 [?\Cx ?l] (list "Cx" "A") [(meta right) (meta left)] (kbd "<f9>") (kbd "<C-f9>") (kbd "CMa") [24 f9 97])) (cl-loop for key in my-example-keys do (princ (my-kbd-inverse key)) (terpri)) 

If there is a key that can be passed to global-set-key but does not work with the current definition of my-kbd-inverse , let me know.

Side note: there is a reason why key-description does not accept a character as an argument. There are two errors:

  • a symbol is just a number. There is no separate data type for characters.

  • it makes sense that the symbol is not a key. A symbol is an input event. A sequence of input events forms a key sequence (key combination), and a key sequence is called short. The character ?a not a key, but the vector [a] and the string "a". The global-set-key and key-description functions accept only keys.

+2
source

All Articles