On my keyboard layout, pressing some key combinations (like M-c as C is where R is on the American keyboard layout) can be quite tricky.

This is why I want Alt Gr or the context menu key to be a meta key. But I don't want to lose Alt as one. Preferably, this change should only occur in Emacs as my keyboard layout uses Alt Gr and having a context menu key is nice. I assume that using the context menu key therefore will cause less trouble.

How can this be achieved?

I only use Ubuntu systems.

  • What window manager or desktop environment do you use? I don't think Emacs has anything built in that would let you do this, but you can change the layout on a per-application basis if your WM/DE supports it. – Gilles Jul 15 '17 at 21:47
  • Do you want AltGr+key to be equivalent to Alt in Emacs for all keys, or only for keys where there is no AltGr character? – Gilles Jul 15 '17 at 21:49

Partial solution: ESC key is equivalent to M-key, so you can type Escape key instead of Alt+key if that's more convenient. If you don't use Menu for anything in Emacs, you can make it an alternate ESC. You'd have to type Menu then the key instead of using it as a modifier.

(define-key function-key-map [menu] [?\e])

An approximately equivalent solution would be

(define-key function-key-map [menu] 'event-apply-meta-modifier)

This one generalizes to other modifiers but has the downsides that it plays tricks with the event loop and that makes it fail rarely, but confusingly (because diagnostic messages will display the applied modifier but some of the lower layers of Emacs won't see it).

Here's a slightly more complex variant that allows keeping the key down, i.e. you can press and hold Menu, then press another key and release both. However, if you want to type two Alt+key successively, you'll still have to release Menu and press it back between the two keys, because Emacs doesn't react to key-up events.

(defun event-force-meta-modifier (_ignore-prompt)
  (let ((event (read-event)))
    (message "event = %S" event)
    (while (eq (lookup-key function-key-map (vector event)) 'event-force-meta-modifier)
      (message "event = %S" event)
      (setq event (read-event)))
    (vector (event-apply-modifier event 'meta 27 "M-"))))
(define-key function-key-map [menu] 'event-force-meta-modifier)

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.