2

I would like to bind the dedicated function keys (F1 through F10) using the standard modifier key, e.g. (define-key my-keymap (kbd "<S-F2>") #'my-function). However, this and similar keybindings have no effect.

describe-key reports: "<f2> (translated from <S-f2>) runs the command. . ." and gives the target command for the unmodified F2 key. This is true for all the function keys on my system (using Windows 10 and Emacs 27.2).

Earlier StackExchange answers about key translation suggest that I must modify one of the translation keymaps. Following some specific advice, I have tried (define-key input-decode-map [f2] 'nil) but evaluating this form has no effect.

More generally, I cannot find where this bulk translation is occurring. Running emacs -q shows that some of the translations, but not all, are set early in the start-up process. With -q, define-key() says that "<f3> (translated from <S-f3>) runs the command kmacro-start-macro-or-insert-counter."

Is there is an easier way to de-translate the function keys? Is there a downside to doing so?

2
  • 1
    Try (define-key my-keymap (kbd "<S-F2>") #'my-function) without the backslash?
    – NickD
    Dec 3, 2021 at 21:23
  • Thanks Nick. That backslash is a typo I made here, on StackExchange. define-key() alas does not override the key translation.
    – Tom Davey
    Dec 3, 2021 at 23:49

1 Answer 1

2
(define-key my-keymap (kbd "<S-F2>") #'my-function)

describe-key reports: "<f2> (translated from <S-f2>)

<S-F2> is not <S-f2>

Note that define-key doesn't know whether the specified key actually exists or not -- you could equally define a binding for <S-jewfjwjpfwmpfm> and it would happily accept it. It's just that in practice neither <S-jewfjwjpfwmpfm> nor <S-F2> will ever be received.

More generally, I cannot find where this bulk translation is occurring

It's a hard-coded feature in the read_key_sequence C code.

2
  • Thanks for jogging my (monotonically decreasing and now almost non-existent) memory... C-h i g(elisp) function keys says: "Most keyboards also have “function keys”—keys that have names or symbols that are not characters. Function keys are represented in Emacs Lisp as symbols; the symbol’s name is the function key’s label, in lower case. For example, pressing a key labeled <F1> generates an input event represented by the symbol ‘f1’."
    – NickD
    Dec 4, 2021 at 1:38
  • Thanks Phil. With the name of the function key put in lower case, define-key() works as expected. SMH. I was terrified I would have to edit esoteric translation keymaps. Answer accepted. I have learned an Emacs troubleshooting lesson: check character case FIRST. Humbling. > "It's a hard-coded feature in the read_key_sequence C code." Whoa, that is very low level, down there in Dante's Seventh Circle of Emacs. Nick, thanks for the Elisp Info Manual reference.
    – Tom Davey
    Dec 4, 2021 at 5:02

Your Answer

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

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