public void componentShown(ComponentEvent evt) {
      // sets the security manager to fix a bug in liveconnect in Safari on Mac
      if (key != -1) {
        return;
      }
      window = (JSObject) JSObject.getWindow(applet());

      AccessController.doPrivileged(
          new PrivilegedAction() {
            public Object run() {
              log("> init Robot");
              try {
                SecurityManager oldsecurity = System.getSecurityManager();
                boolean isOpera = false;
                try {
                  isOpera = (System.getProperty("browser").equals("Opera.plugin"));
                } catch (Exception e) {
                }
                try {
                  securitymanager = oldsecurity;
                  securitymanager.checkTopLevelWindow(null);
                  // xdomain
                  if (charMap == null) {
                    if (!confirm(
                        "DOH has detected that the current Web page is attempting to access DOH, but belongs to a different domain than the one you agreed to let DOH automate. If you did not intend to start a new DOH test by visiting this Web page, press Cancel now and leave the Web page. Otherwise, press OK to trust this domain to automate DOH tests.")) {
                      stop();
                      return null;
                    }
                  }
                  log("Found old security manager");
                } catch (Exception e) {
                  e.printStackTrace();
                  log("Making new security manager");
                  securitymanager = new RobotSecurityManager(isOpera, oldsecurity);
                  securitymanager.checkTopLevelWindow(null);
                  System.setSecurityManager(securitymanager);
                }
                // instantiate the Robot
                robot = new Robot();
              } catch (Exception e) {
                log("Error calling _init_: " + e.getMessage());
                key = -2;
                e.printStackTrace();
              }
              log("< init Robot");
              return null;
            }
          });
      if (key == -2) {
        // applet not trusted
        // start the test without it
        window.eval("doh.robot._appletDead=true;doh.run();");
      } else {
        // now that the applet has really started, let doh know it's ok to use it
        log("_initRobot");
        dohrobot = (JSObject) window.eval("doh.robot");
        dohrobot.call("_initRobot", new Object[] {applet()});
      }
    }
 public boolean hasFocus() {
   try {
     return ((Boolean)
             window.eval(
                 "var result=false;if(window.parent.document.hasFocus){result=window.parent.document.hasFocus();}else{result=true;}result;"))
         .booleanValue();
   } catch (Exception e) {
     // runs even after you close the window!
     return false;
   }
 }
  public void _initKeyboard(double sec) {
    log("> initKeyboard");
    // javascript entry point to discover the keyboard
    if (!isSecure(sec)) return;
    if (charMap != null) {
      dohrobot.call("_onKeyboard", new Object[] {});
      return;
    }

    AccessController.doPrivileged(
        new PrivilegedAction() {
          public Object run() {
            charMap = new HashMap();
            KeyEvent event = new KeyEvent(applet(), 0, 0, 0, KeyEvent.VK_SPACE, ' ');
            charMap.put(new Integer(32), event);
            try {
              // a-zA-Z0-9 + 29 others
              vkKeys = new Vector();
              for (char i = 'a'; i <= 'z'; i++) {
                vkKeys.add(
                    new Integer(
                        KeyEvent.class
                            .getField("VK_" + Character.toUpperCase((char) i))
                            .getInt(null)));
              }
              for (char i = '0'; i <= '9'; i++) {
                vkKeys.add(
                    new Integer(
                        KeyEvent.class
                            .getField("VK_" + Character.toUpperCase((char) i))
                            .getInt(null)));
              }
              int[] mykeys =
                  new int[] {
                    KeyEvent.VK_COMMA,
                    KeyEvent.VK_MINUS,
                    KeyEvent.VK_PERIOD,
                    KeyEvent.VK_SLASH,
                    KeyEvent.VK_SEMICOLON,
                    KeyEvent.VK_LEFT_PARENTHESIS,
                    KeyEvent.VK_NUMBER_SIGN,
                    KeyEvent.VK_PLUS,
                    KeyEvent.VK_RIGHT_PARENTHESIS,
                    KeyEvent.VK_UNDERSCORE,
                    KeyEvent.VK_EXCLAMATION_MARK,
                    KeyEvent.VK_DOLLAR,
                    KeyEvent.VK_CIRCUMFLEX,
                    KeyEvent.VK_AMPERSAND,
                    KeyEvent.VK_ASTERISK,
                    KeyEvent.VK_QUOTEDBL,
                    KeyEvent.VK_LESS,
                    KeyEvent.VK_GREATER,
                    KeyEvent.VK_BRACELEFT,
                    KeyEvent.VK_BRACERIGHT,
                    KeyEvent.VK_COLON,
                    KeyEvent.VK_BACK_QUOTE,
                    KeyEvent.VK_QUOTE,
                    KeyEvent.VK_OPEN_BRACKET,
                    KeyEvent.VK_BACK_SLASH,
                    KeyEvent.VK_CLOSE_BRACKET,
                    KeyEvent.VK_EQUALS
                  };
              for (int i = 0; i < mykeys.length; i++) {
                vkKeys.add(new Integer(mykeys[i]));
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
            Thread thread =
                new Thread() {
                  public void run() {
                    robot.setAutoDelay(0);
                    log("< initKeyboard");
                    pressNext();
                  }
                };
            thread.start();
            return null;
          }
        });
  }