Java Robot不要输入@

时间:2019-02-21 16:24:27

标签: java sign awtrobot

嘿,我想为电子邮件和CO创建自己的自动登录,作为练习,因为我还是一个初学者。 我为此使用Java机器人,因为我知道它没有所有的特殊字符,所以我想在机器人获取它们之前捕获一些字符。我也这样做(我认为)。但是我总是收到“无效的密钥代码”错误。有人有建议吗 ? 我用的是德国键盘盖

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);
      }
    }


}

线程“ main”中的异常java.lang.IllegalArgumentException:无效的键代码

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)

0 个答案:

没有答案