How to remap 'Context Menu' key on external keyboard?

I'm using a Cherry G80 (excellent 'clicky' keyboard btw.) on my Macbook Air. Everything works fine, I can use and remap all keys using Ukelele1 or directly in Emacs.

The only key behaving strange is the Context Menu2 Key.

It returns the same Unicode sequence as Control-P.

This is the output of Key Codes3 when pressing the Context Menu key:

Key Down
Characters: 
Unicode:        16 / 0x10
Keys:       
Key Code:   110 / 0x6e
Modifiers:  256 / 0x100

This is the output for Control-P:

Key Down
Characters: 
Unicode:        16 / 0x10
Keys:       ⌃P
Key Code:   35 / 0x23
Modifiers:  262401 / 0x40101

(Notice the difference in 'Keys')

My goal is to use it directly in Emacs or be able to swap it to some other (modifier) key. Is there any way to do this? I can't find a way to bring Ukelele to do that.


Solution 1:

Bad news. Apple's XML DTD format that Ukelele targets does not support modifiers outside of shift keys, option keys, control keys, and command keys.

As @Lri suggested KeyRemap4MacBook supports mapping the Context Menu Key, which it calls the Application Key. In KeyRemap4MacBook's preferences go to For PC Users -> Change PC Application Key to see all the supported options. Mappings that may be of use for Emacs: Fn Key; Shift_L Key; Option_L Key; Control_L Key; and, Command_L Key.

Solution 2:

This is what I've done to map the menu button on my keyboard to the option modifier:

hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000065,"HIDKeyboardModifierMappingDst":0x7000000e6}]}'

The approach I used for to first find the key code for the keyboard button. Use https://developer.apple.com/library/archive/technotes/tn2450/_index.html for the list of key codes. To find the key code for button I wanted to change I used

hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000065,"HIDKeyboardModifierMappingDst":0x700000004}]}'

which converts the button to the letter 'a.' That way, when I pressed the key on the keyboard, the letter 'a' should appear in the terminal. I kept changing HIDKeyboardModifierMappingDst until I found the key code that I was looking for. Once I knew the source code, I used the table in the link above to figure out what to change it to.