How do you put a String variable in the Robot Class KeyEvent parameter?

Viewed 1940

I am using the Robot Class in Java, and I created a static method for pressing a key. My problem is that I want to be able to press whichever key using input from my main method, but the parameters for the keyPress method and keyRelease method do not accept String. And I don't want to use a lot of if statements to press that key. Here is my code:

public static void keyType(String key) throws AWTException {
  Robot r = new Robot();
  r.keyPress(KeyEvent.VK_A);
  r.keyRelease(KeyEvent.VK_A);
}

Now I want to use the key variable (input from my main method) to decide which key to press. I planned to just use a capital letter in the String (such as "A", or "B"), and replace the (KeyEvent.VK_A) in the keyPress and keyRelease parameters with (KeyEvent.VK_ + key). But then I realized that it didn't accept or use String parameters, so what do I do?

2 Answers
Related