4

I have Emacs 26.1 from emacsformacosx.com set up to use left option as meta, and to ignore right option, so as to leave it available for accented characters etc.

It works fine on my MacBook Pro's built-in keyboard, and was working fine with an external PC keyboard in High Sierra. But since I installed Mojave (a reformat and clean install), the external keyboard is backwards — left option is ignored, right option is meta.

The keyboard is a CoolerMaster QuickFire Rapid. As a further complication, I have option and command switched via Keyboard Preferences / Keyboard / Modifier Keys.

  1. what else can I do to diagnose the problem? Is there a way I can tell what keycodes the keys are sending? (The Keyboard Viewer highlights both option keys whenever one is pressed, so it's no help.)
  2. is there a way to intentionally swap the keys, e.g. in ~/Library/KeyBindings/, but only for that specific keyboard?
  3. is there a way to at least detect what keyboard is installed, e.g. in a bash script, which might allow me to partially hack around the problem in my init.el? (It wouldn't be ideal to have to relaunch emacs when I switch keyboards, but it would be better than nothing.)

Note: I don't really want to install any third-party keyboard mapping software (Karabiner etc.) just to deal with this problem, although I suppose I'd be open to it for the diagnosis part.


Updated to add: Per @nohillside's comment, I tried disabling the option/command switch in Keyboard Preferences. This fixed the left/right problem. It's not a workable solution, but it does seem to isolate the problem to the modifier key customization.


Updated again: I upgraded an iMac that uses the same PC keyboard to Mojave, and it doesn't exhibit the problem; it may be specific to portables (or even to that particular MacBook Pro).


Updated yet again: Since updating to Catalina, the iMac now has the same problem.


Eventual solution: Got a Drop CTRL programmable keyboard (there are cheaper options) with QMK firmware, and remapped the keys on the keyboard side, instead of depending on the Mac's software switch.

David Moles
  • 539
  • 6
  • 24
  • 1
    Can you disable all the modifications and see how it works then? And afterwards add them step by step? – nohillside Oct 09 '18 at 15:42
  • @nohillside Good call, see update above. – David Moles Oct 09 '18 at 16:20
  • I have the same problem, with both ThinkPad USB/BT keyboards. It happens since High Sierra. – amdyes Oct 10 '18 at 06:31
  • Mine was not reversed but the Windows lock was activated and I didn't realize that. – Tsuharesu Oct 10 '18 at 13:34
  • On the same boat with latest MBP 15" + Mojave. Internal/built-in keyboard is set to swap alt and cmd. Then, two alts are swapped with the alt-cmd swapping option on all external keyboards. (tried three different keyboards). Probably your iMac is not set to swap alt-cmd, as you never use an apple keyboard with it? – krim Dec 20 '18 at 03:39
  • @krim I use the internal keyboard, but I only swap the keys when using an external PC keyboard (non-Apple). – David Moles Dec 20 '18 at 17:39
  • David, it's a stretch given this question is almost two years old, but did you ever fix this? I'm having this exact problem now. – Purag Aug 26 '20 at 10:29
  • @Purag I never have. My current thought (which I haven't had time to look into) is to use ioreg to tell when I have my external keyboard connected, and use that as a hook in init.el. – David Moles Aug 26 '20 at 16:45
  • Please add your recent edit as an answer. – Allan Feb 24 '23 at 22:31
  • @Allan if you mean “buy a keyboard”, that’s the kind of “answer” I usually downvote and flag. – David Moles Feb 26 '23 at 01:19

1 Answers1

3

Switching Command and Option in recent macOS versions for some reason also swaps Left/Right Option. Thankfully, it is possible to manually configure which keys to swap by means of the defaults command line tool.

  1. First, find out what the vendor and product IDs are for your external keyboard:

    ioreg -p IOUSB -c IOUSBDevice | grep -e class -e idVendor -e idProduct
    

    I have a Microsoft Wired 600 keyboard, for example:

    ...
    | ...
    +-o Wired Keyboard 600@14200000  <class AppleUSBDevice, id 0x1000003a3, registered, matched, active, busy 0 (0 ms), retain 13>
          "idProduct" = 1872
          "idVendor" = 1118
    
  2. Delete any existing modifier key mappings:

    defaults -currentHost delete -g com.apple.keyboard.modifiermapping.<vendor-ID>-<product-id>-0
    

    For my Wired 600 keyboard:

    defaults -currentHost delete -g com.apple.keyboard.modifiermapping.1118-1872-0
    
  3. Swap the Command/Option modifier keys (left to left, right to right):

    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1118-1872-0 -array-add '<dict><key>HIDKeyboardModifierMappingDst</key><integer>30064771299</integer><key>HIDKeyboardModifierMappingSrc</key><integer>30064771298</integer></dict>'
    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1118-1872-0 -array-add '<dict><key>HIDKeyboardModifierMappingDst</key><integer>30064771298</integer><key>HIDKeyboardModifierMappingSrc</key><integer>30064771299</integer></dict>'
    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1118-1872-0 -array-add '<dict><key>HIDKeyboardModifierMappingDst</key><integer>30064771302</integer><key>HIDKeyboardModifierMappingSrc</key><integer>30064771303</integer></dict>'
    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1118-1872-0 -array-add '<dict><key>HIDKeyboardModifierMappingDst</key><integer>30064771303</integer><key>HIDKeyboardModifierMappingSrc</key><integer>30064771302</integer></dict>'
    
  4. Replug your keyboard or log out/in to make the settings take effect.

You can also disable the Caps Lock key, if you want:

defaults -currentHost write -g com.apple.keyboard.modifiermapping.1118-1872-0 -array-add '<dict><key>HIDKeyboardModifierMappingDst</key><integer>30064771072</integer><key>HIDKeyboardModifierMappingSrc</key><integer>30064771129</integer></dict>'

Here's a list of the IDs for they keys remapped here:

  decimal    hexadecimal
-----------  -----------
30064771298  0x7000000e2  Left Alt
30064771299  0x7000000e3  Left Cmd
30064771302  0x7000000e6  Right Alt
30064771303  0x7000000e7  Right Cmd
30064771129  0x700000039  Caps Lock
30064771072  0x700000000  <no action>

Some more information can be found here:

PS. You can look at the ~/Library/Preferences/ByHost/.GlobalPreferences.XXXXXXXX-YYYY-ZZZZ-WWWW-VVVVVVVVVVVV.plist file to see the current mappings. The long ID will be specific to your computer. TextMate, for example, will open plist files.

Brecht Machiels
  • 354
  • 2
  • 13