title: Java Robot Class KeyEvent
body: <p>Is there a way to write the line</p>
  
  <pre><code>r.keyPress(KeyEvent.VK_SPACE);
  </code></pre>
  
  <p>that will allow an html 5 game running in safari to register a spacebar press as having occurred inside the game? (by inside the game, I mean have the game react as if "space" was physically pressed)</p>
  
  <p>For example, I have realized that some KeyEvents only register as having fired when over top of certain elements. The code below will successfully register a spacebar-press event in the URL bar of safari: </p>
  
  <pre><code>import java.awt.Robot;
  import java.awt.AWTException;
  import java.awt.event.KeyEvent;
  
   public class SpaceBarPress {
  
      SpaceBarPress(){
      }
  
      public static void main (String[] args) {
          try{
              Robot r = new Robot();  //create instance of robot class
              r.mouseMove(600,323); //move cursor over top URL bar
  
              r.mousePress(InputEvent.BUTTON1_MASK); //bring URL bar into focus
              r.mouseRelease(InputEvent.BUTTON1_MASK);
  
              r.keyPress(KeyEvent.VK_SPACE); //fire spacebar event
              r.keyRelease(KeyEvent.VK_SPACE);
  
            }
            catch(AWTException e){
                e.printStackTrace();
            }
        }
    }
  </code></pre>
  
  <p>However, when the same code is run overtop an html 5 game running in the browser, a spacebar event will not register inside the game. Whereas, pressing the physical spacebar will have an effect on the game. </p>