I’m trying to automate some processes using Robot and it seems certain keycodes (only symbols that require you to hold shift when typing it normally) in KeyEvent are throwing an IllegalArgumentException. This is all the code that’s running in main:
Robot r = new Robot();
r.keyPress(KeyEvent.VK_EXCLAMATION_MARK);
However, it works fine using the following workaround:
Robot r = new Robot();
r.keyPress(KeyEvent.VK_SHIFT);
r.keyPress(KeyEvent.VK_1);
Any ideas why the exception is thrown? Thanks!
Java version: 1.6.0_23
Because like the documentation for
Robot.keyPresssays, anIllegalArgumentExceptionis thrown when thekeycodedoesn’t represent a valid key, andVK_EXCLAMATION_MARKis not a valid key.Keycodes are used to represent two things: keys on the keyboard, and “a character was typed” events. Typing a character often requires more than one keypress (in sequence, or simultaneously, or both). But
Robot.keyPresssimulates the act of pressing a key (hence the name), not the act of typing a character.For more information, see the documentation for KeyEvent: http://download.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html