title: Java: Convert string to KeyEvent virtual key code?
body: <p>I'm trying to write a method that will take a single character string and (if possible) return the <a href="http://docs.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html" rel="nofollow">virtual key code</a> it corresponds to.</p>
  
  <p>For instance:</p>
  
  <pre><code>private static int getKeyCode(final String key) {
      if(key.length != 1)
          throw new IllegalArgumentException("Only support single characters");
  
      // Also check to see if the 'key' is (1-9)(A-Z), otherwise exception
  
      // How to perform the conversion?
  }
  
  // Returns KeyEvent.VK_D
  MyKeyUtils.getKeyCode("D");
  </code></pre>
  
  <p>Thus, passing <code>MyKeyUtils.getKeyCode("blah")</code> throws an <code>IllegalArgumentException</code> because "blah" has 4 chars. Also, passing <code>MyKeyUtils.getKeyCode("@")</code>throws the same exception because "@" is neither a digit 0-9 or a character A - Z.</p>
  
  <p>Any ideas how to do the regex check as well as the actual conversion? Thanks in advance!</p>