1

hey i want to create my own auto login for emails and CO, as an exercise because i am still a beginner. I use the Java Robot for this and since I know that it doesn't have all the special characters I want to catch some of the characters before the robot gets them. I do that too (I think).But I always get the "Invalid key code" error. Does anyone have any advice ? And i use a german keybord

import java.io.IOException;
import java.awt.*;
import java.awt.event.*;

    public class Login {

        public static void main (String [] args) {
    /*  args 0 is for the website
        args 1 is for the email adresse
        args 2 is for the password
        */
        try {
//            Process p1= Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\"");
              Process p1= Runtime.getRuntime().exec("\"C:\\ProgramFiles \\Notepad++\\notepad++.exe\""); // i use notepad here for testing
              Thread.sleep(100);    

              write(args[0]);
              Thread.sleep(300);

              write(args[1]);
              Thread.sleep(300);

              write(args[2]);

//              p1.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public static void write(String s)
    {
      try
      {
        Robot r = new Robot();
        int i = 0;
        char[] buchstaben = s.toCharArray();
        String AT = "@";


        while (i<s.length())
        {
          System.out.println("KeyEvent ist: "+ buchstaben[i]);

          if((String.valueOf(buchstaben[i]).contains(AT))==true)
          {

              r.keyPress(KeyEvent.VK_AT);
//            r.keyPress(KeyEvent.VK_ALT_GRAPH);
//            r.keyPress('Q');
              r.delay(250);
              r.keyRelease(KeyEvent.VK_AT);
//            r.keyRelease(KeyEvent.VK_ALT_GRAPH);
//            r.keyRelease(KeyEvent.VK_Q);
              i++;
          }


          if(Character.isUpperCase(buchstaben[i]))
          {
             r.keyPress(KeyEvent.VK_SHIFT);
          }
          synchronized(r)
          {

            r.keyPress(Character.toUpperCase(buchstaben[i]));
            r.delay(250);
            r.keyRelease(Character.toUpperCase(buchstaben[i]));
          }
          if(Character.isUpperCase(buchstaben[i]))
          {
             r.keyRelease(KeyEvent.VK_SHIFT);
          }
          i++;
        }

        r.keyPress(KeyEvent.VK_ENTER);
        r.delay(250);
        r.keyRelease(KeyEvent.VK_ENTER);
      }
      catch(AWTException e)
      {
        System.err.println(e);
      }
    }


}

Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code

at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Robot.java:354)
at Login.write(Login.java:53)
at Login.main(Login.java:21)
luator
  • 4,769
  • 3
  • 30
  • 51
Anton Hinkel
  • 111
  • 4
  • 1
    side note: `if((String.valueOf(buchstaben[i]).contains(AT))==true)` could be simplified to `if( buchstaben[i] == '@' )` (or `... == AT` if you use `char AT = '@';`). – Thomas Feb 21 '19 at 16:29
  • `I always get the "Invalid key code" error.` - I assume a `IllegalArgumentException` is being thrown from `Robot.checkKeycodeArgument(keycode)` or one of its peers. If so can you provide the full stacktrace and mark the line of your code that's mentioned there? – Thomas Feb 21 '19 at 16:34
  • i had tried this before but wasn't sure if it was part of the problem or not. with this way i'm sure that it works – Anton Hinkel Feb 21 '19 at 16:35
  • Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code at sun.awt.windows.WRobotPeer.keyPress(Native Method) at java.awt.Robot.keyPress(Robot.java:354) at Login.write(Login.java:53) at Login.main(Login.java:21) – Anton Hinkel Feb 21 '19 at 16:36
  • Please [edit] your question and add the stracktrace there (nicely formatted please). Also please tell us which line line 53 is. – Thomas Feb 21 '19 at 16:39
  • The problem seems to be a native method so it's hard to tell what the peer doesn't like. However, this might help you: https://stackoverflow.com/questions/22919591/robot-keypress-doesnt-work-for-vk-alt-graph-invalid-key-code (first result for "WRobotPeer invalid key") – Thomas Feb 21 '19 at 16:42
  • that's it, I only had to add the line r.keyPress(KeyEvent.VK_CONTROL); thank you very much. – Anton Hinkel Feb 21 '19 at 16:51
  • Isn't this answered in https://stackoverflow.com/questions/17844378/invalid-key-code-java – Aleksandr Charkov Feb 21 '19 at 16:52

0 Answers0