1

I have a very simple question, or at least I think it is simple. Currently I'm trying to use the robot class with variables. What I mean by this is the following (the variables "pass" are chars.)

            pass1 = 0;
            pass2 = 0;
            pass3 = 0;
            pass4 = 0;


                try{
                        Robot robot = new Robot(); 
                        robot.delay(2000);

                        robot.mouseMove(1318, 322);
                        robot.keyPress(pass1);
                        robot.keyPress(pass2);
                        robot.keyPress(pass3);
                        robot.keyPress(pass4);
                        } catch (AWTException e) {e.printStackTrace();}

When I run the program, I get this error (keep in mind I only get this error when the portion of the code with "pass1, pass2...etc." is in it.):

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code
at java.awt.Robot.checkKeycodeArgument(Unknown Source)
at java.awt.Robot.keyPress(Unknown Source)
at Cracker$2.mouseReleased(Cracker.java:117)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
3
  • If pass1-pass4 are chars, shouldn't they be pass1 = '0', etc? Commented Mar 4, 2014 at 0:52
  • Is 0 a valid value? Because that's what you're passing to every keyPress(pass#) Commented Mar 4, 2014 at 0:53
  • @solace The integer 0, is convertible to a char. That still doesn't necessarily make them valid argument values for the robot method. Commented Mar 4, 2014 at 0:54

1 Answer 1

1

Robot#keyPress expects an int, which represents the virtual key code as described in java.awt.KeyEvent

Specifically, key 0 represents KeyEvent.VK_UNDEFINED which is an "invalid key code"

Don't forget that until you call keyRelease, the keys will continue to be pressed, generating repeated key pressed events...

Sign up to request clarification or add additional context in comments.

4 Comments

Do you have an easy way to fix this? So far I can't think of one.
The question is, why are sending 0 to the keyPress method and what is it you are hoping to achieve?
I'm trying to make the KeyPress method type "0" or "1"
Use bot.keyPress(KeyEvent.VK_0);, bot.keyRelease(KeyEvent.VK_0) and use KeyEvent.VK_1 for number "1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.