Type a String using java.awt.Robot

前端 未结 7 1867
难免孤独
难免孤独 2020-12-10 00:46

I already know how to use java.awt.Robot to type a single character using keyPress, as seen below. How can I simply enter a whole

7条回答
  •  感动是毒
    2020-12-10 01:07

    This doesn't type the entire "string" but helps to type whatever you want other than one character at a time.

        Runtime.getRuntime().exec("notepad.exe");//or anywhere you want.
       Thread.sleep(5000);//not required though gives a good feel.
        Robot r=new Robot();      
         String a="Hi My name is Whatever you want to say.";
        char c;
         int d=a.length(),e=0,f=0;
    
         while(e<=d)
        {
         c=a.charAt(e);
         f=(int) c; //converts character to Unicode. 
          r.keyPress(KeyEvent.getExtendedKeyCodeForChar(f));
         e++;
           Thread.sleep(150);
    
           }
    

    see it works perfectly and it's awesome! Though it doesn't work for special characters which cannot be traced by unicode like |,!...etc.

提交回复
热议问题