title: How to detect if a key was pressed in Java?
body: <p>I am a beginner in Java an I have been looking on how to detect if the user pressed a key (such a the arrow keys). Apparently there are a lot of ways to do such a thing, and I found that this method should work for me: </p>
  
  <pre><code>public void keyPressed(KeyEvent e) {
      int keyCode = e.getKeyCode();
      switch( keyCode ) { 
          case KeyEvent.VK_UP:
              // handle up 
              break;
          case KeyEvent.VK_DOWN:
              // handle down 
              break;
          case KeyEvent.VK_LEFT:
              // handle left
              break;
          case KeyEvent.VK_RIGHT :
              // handle right
              break;
       }
  }
  </code></pre>
  
  <p>The problem is that I have no idea what a KeyEvent is.<br>
  Can anyone tell me what to put in the parentheses when I call the method and show me an example please?</p>
  
  <p>PS: don't send me to an other site, I probably have already looked at it and they just confuse me more...</p>