Disable Hardware Keyboard for iOS Simulator using UIAutomation

If I understood your question correctly, you need bulletproof way to enter text. I just use setValue for that. Like this: UIATarget.localTarget().frontMostApp().textFields().Login.setValue('sofa');


Because Ian's script is not visible in the net anymore , here my variant of the Apple script switching off the hardware keyboard.

tell application "Simulator"
    activate
end tell

tell application "System Events"
    set hwKB to value of attribute "AXMenuItemMarkChar" of menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "Hardware" of menu bar 1 of application process "Simulator"
    if ((hwKB as string) is equal to "missing value") then
      do shell script "echo 'hardware keyboard is off'"
    else
      click menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "Hardware" of menu bar 1 of application process "Simulator"
    end if
end tell

Updated AppleScript of Leo's answer for Xcode 11 where "Hardware" was changed to "I/O". To get this working it Xcode 11.4.1 I needed a bash script wrapper to call the AppleScript scpt file.

Files
./disable-hardware-keyboard
./disable-hardware-keyboard.scpt

tell application "Simulator"
    activate
end tell

tell application "System Events"
    set hwKB to value of attribute "AXMenuItemMarkChar" of menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "I/O" of menu bar 1 of application process "Simulator"
    if ((hwKB as string) is equal to "missing value") then
        do shell script "echo 'hardware keyboard is off'"
    else
        click menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "I/O" of menu bar 1 of application process "Simulator"
    end if
end tell
#!/bin/bash
cd $(dirname $0)
osascript disable-hardware-keyboard.scpt