Can we send keys to the iOS simulator itself, not the AUT?

I need to access some of the simulator functions, specifically the clipboard key combos.

NOTE that the Mac and simulator clipboards are separate!

I need to copy some text from a text field in my app and get it onto the Mac’s clipboard. To do this manually, you highlight the text in your app, send a command+c to copy it onto the simulator’s clipboard, and then choose Edit->Copy from the simulator’s menu to copy from the simulator clipboard to the Mac clipboard.

Programmatically, I can do all but choose Edit->Copy from the simulator’s menu. So I need to send a command+c to the simulator itself, not the app running on the simulator. Does anyone know how to do that? Perhaps someone has done something similar, such as sending a command+h to the simulator to get to the home screen?

public static String simulatorAppName() {
    return "Simulator";
}
public void tapHomeButton() {
    System.out.println("   tapHomeButton()");
    try {
        String[] cmd = {"osascript", "-e", "tell application \""+simulatorAppName()+"\"\n" +
                "    activate\n" +
                "end tell\n" +
                " \n" +
                "tell application \"System Events\"\n" +
                "    tell process \""+simulatorAppName()+"\"\n" +
                "        tell menu bar 1\n" +
                "            tell menu bar item \"Hardware\"\n" +
                "                tell menu \"Hardware\"\n" +
                "                    click menu item \"Home\"\n" +
                "                end tell\n" +
                "            end tell\n" +
                "        end tell\n" +
                "    end tell\n" +
                "end tell"};
        executeShell(cmd);
        try{Thread.sleep(500);}catch (Exception e1){}
    } catch (Exception e) {}
}
1 Like