MacBook booting with wrong keyboard layout

Things you could try:

  • Run sudo rm ~/Library/Preferences/com.apple.HIToolbox.plist ~/Library/Preferences/ByHost/com.apple.HIToolbox.*.plist /Library/Preferences/com.apple.HIToolbox.plist and restart. The current keyboard layout is stored in the first file in 10.9 and the second file in 10.8. The keyboard layout used in the login window and by the guest account is stored in the third file.
  • Run open -a KeyboardSetupAssistant to identify the keyboard.
  • Run sudo rm /var/db/.AppleSetupDone;sudo /System/Library/CoreServices/Setup\ Assistant.app/Contents/MacOS/Setup\ Assistant and create a new account. Set the default keyboard layout to Swiss German in the second step and enter anything else in the other steps. You can delete the account after you have created it.

I had this problem very often, though not always. I think it happened mainly (or always?) when the system was not shutdown or rebooted cleanly. Like in the case of a power loss.

Fixing my config and then copying my fixed $HOME/Library/Preferences/com.apple.HIToolbox.plist file to /Library/Preferences/ didn't work. The system settings file got overwritten on reboot.

What worked for me was to import my user keyboard settings to the default system settings with sudo :

sudo defaults import com.apple.HIToolbox.plist $HOME/Library/Preferences/com.apple.HIToolbox.plist

(This is with High Sierra. I don't know if it works the same with Mojave or Catalina)