mathiasbynens / custom.keylayout

Custom QWERTY/AZERTY .keylayout files for use with Apple keyboards
https://mths.be/keylayout
MIT License
65 stars 9 forks source link

Better way to set default keyboard? #7

Open mathiasbynens opened 10 years ago

mathiasbynens commented 10 years ago

This is for OS X 10.8, but it might just work for 10.9 as well: https://github.com/hjuutilainen/dotfiles/blob/9198bacc8e8ea63bf441a3e313a93a2159037a34/bin/osx-system-defaults.sh#L84-L96

# ==============================================
# Set keyboard preferences
# ==============================================
defaults write /Library/Preferences/com.apple.HIToolbox AppleCurrentKeyboardLayoutInputSourceID "com.apple.keylayout.Finnish"
defaults write /Library/Preferences/com.apple.HIToolbox AppleDefaultAsciiInputSource -dict InputSourceKind "Keyboard Layout" "KeyboardLayout ID" -int 17 "KeyboardLayout Name" Finnish

# Delete the default layouts (US)
defaults delete /Library/Preferences/com.apple.HIToolbox AppleEnabledInputSources

# Enable Finnish layout
defaults write /Library/Preferences/com.apple.HIToolbox AppleEnabledInputSources -array '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 17; "KeyboardLayout Name" = Finnish; }'
defaults write /Library/Preferences/com.apple.HIToolbox AppleInputSourceHistory -array '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 17; "KeyboardLayout Name" = Finnish; }'
defaults write /Library/Preferences/com.apple.HIToolbox AppleSelectedInputSources -array '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 17; "KeyboardLayout Name" = Finnish; }'
Lendar commented 9 years ago

It didn't work on Yosemite 10.10: the new input source was still missing after restart.

I had to encode values in XML using that example on stackoverflow.

defaults write com.apple.HIToolbox AppleEnabledInputSources -array '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>0</integer><key>KeyboardLayout Name</key><string>U.S.</string></dict>' '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>19967</integer><key>KeyboardLayout Name</key><string>Russian-BG46</string></dict>'

After that the new input source was enabled by OS X. Restart was still needed in Yosemite, and re-log still wasn't enough.

Macxim commented 7 years ago

FIY, this still doesn't work on Sierra 10.12.4.

# Enable French and Spanish layout
defaults write /Library/Preferences/com.apple.HIToolbox AppleEnabledInputSources -array \
 '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 1; "KeyboardLayout Name" = French; }' \
 '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 8; "KeyboardLayout Name" = Spanish; }' \

defaults write /Library/Preferences/com.apple.HIToolbox AppleInputSourceHistory -array \
 '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 1; "KeyboardLayout Name" = French; }' \
 '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 8; "KeyboardLayout Name" = Spanish; }' \

defaults write /Library/Preferences/com.apple.HIToolbox AppleSelectedInputSources -array \
 '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 1; "KeyboardLayout Name" = French; }' \
 '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 8; "KeyboardLayout Name" = Spanish; }' \

Whereas this seem to work after restarting.

defaults write com.apple.HIToolbox AppleEnabledInputSources -array-add '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>1</integer><key>KeyboardLayout Name</key><string>French</string></dict>'

defaults write com.apple.HIToolbox AppleEnabledInputSources -array-add '<dict><key>InputSourceKind</key><string>Keyboard Layout</string><key>KeyboardLayout ID</key><integer>8</integer><key>KeyboardLayout Name</key><string>Spanish</string></dict>'