Xcode Swift UI Test - Simulator hardware keyboard

There is a plist with simulator preferences located at ~/Library/Preferences

To change it to turn off the hardware keyboard, make sure the simulator is off, then run this command:

defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool no

You can use xcrun simctl options in a script to shut down simulators.

EDIT

Apple changed this sometime in 2018-2019, I found this answer somewhere else on SO, it works for me:

/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print $1 if /^    (\S*) =/' | while read -r a; do /usr/libexec/PlistBuddy -c "Set :DevicePreferences:$a:ConnectHardwareKeyboard false" ~/Library/Preferences/com.apple.iphonesimulator.plist || /usr/libexec/PlistBuddy -c  "Add :DevicePreferences:$a:ConnectHardwareKeyboard bool false" ~/Library/Preferences/com.apple.iphonesimulator.plist; done

For newer versions of Xcode including XCODE 11. This works nicely. Add it to the pre-actions of the UITest target as a run script:

xcrun simctl shutdown ${TARGET_DEVICE_IDENTIFIER}
plutil -replace DevicePreferences.${TARGET_DEVICE_IDENTIFIER}.ConnectHardwareKeyboard -bool NO ~/Library/Preferences/com.apple.iphonesimulator.plist