Robot class java , typing a string issue

ⅰ亾dé卋堺 提交于 2021-02-05 06:11:46

问题


I m using the following loop , but its only typing the first charecter and the rest as numbers, any idea ?

import java.awt.*;

import javax.swing.KeyStroke;

public class test {

    public static void main(String[] args) throws AWTException
    {
        Robot r = new Robot();

        String s = "Face";

        for (int i = 0; i < s.length(); i++) 
        {
            char res = s.charAt(i);
            r.keyPress(res);
            r.keyRelease(res);  
            r.delay(1000);
        }           
    }
}

OUTPUT typing : F135


回答1:


The keyPress/Release methods need an int value that represents the character you want to type. These value are the key code for each character as determined by the KeyEvent.VK_??? variables.

Try:

import java.awt.*;
import java.util.*;
import java.lang.reflect.Field;
import java.awt.event.*;
import javax.swing.*;

public class RobotCharacter
{
    public static void main(String[] args)
        throws Exception
    {
        JTextField textField = new JTextField(10);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( textField );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        Robot robot = new Robot();
        typeCharacter(robot, "a");
        typeCharacter(robot, "b");
        typeCharacter(robot, "C");
        typeCharacter(robot, "D");
     }

    public static void typeCharacter(Robot robot, String letter)
    {
        try
        {
            boolean upperCase = Character.isUpperCase( letter.charAt(0) );
            String variableName = "VK_" + letter.toUpperCase();

            Class clazz = KeyEvent.class;
            Field field = clazz.getField( variableName );
            int keyCode = field.getInt(null);

            robot.delay(1000);

            if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );

            robot.keyPress( keyCode );
            robot.keyRelease( keyCode );

            if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

However, even this won't work for all characters. For example on my keyboard the "%" is above the "5". You can't use VK_PERCENT. The key stroke needed is VK_5 along with a shift. There is no way to know the actual mapping of your keyboard to do this automatically.

So a Robot is not a good way to do this.




回答2:


The Robot class uses key codes which are defined here: http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/KeyEvent.html, not raw characters. You need to call it like this:

r.keyPress(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_A);



回答3:


@ camickr I made a small edit to your scipt to accomodate a string rather than a letter . Please find below . It helped me Thanks :) function call : typeCharacter(robot, "thanks");

public static void typeCharacter(Robot robot, String letter)
      {

     for(int i=0;i<letter.length();i++){
      try
      {
          boolean upperCase = Character.isUpperCase( letter.charAt(i) );
          String KeyVal=Character.toString(letter.charAt(i));
          String variableName = "VK_" + KeyVal.toUpperCase();
          Class clazz = KeyEvent.class;
          Field field = clazz.getField( variableName );
          int keyCode = field.getInt(null);

          robot.delay(1000);

          if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );

          robot.keyPress( keyCode );
          robot.keyRelease( keyCode );

          if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
      }
      catch(Exception e)
      {
          System.out.println(e);
      }
      }

  }



回答4:


You could also load your String to the clipboard and just paste it where ever you want to.

                StringSelection selection = new StringSelection("Hello World");
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                clipboard.setContents(selection, selection);

                robo.keyPress(KeyEvent.VK_CONTROL);
                robo.keyPress(KeyEvent.VK_V);
                robo.keyRelease(KeyEvent.VK_V);
                robo.keyRelease(KeyEvent.VK_CONTROL);



回答5:


You can make your life easy if you create a function receiving a string to be typed:

public void keyboardString(final String text) {
    if (text != null) {
        try {
            final Robot robot = new Robot();
            for (int i = 0; i < text.length(); i++) {
                final char ch = text.charAt(i);
                final boolean upperCase = Character.isUpperCase(ch);
                final int keyCode = KeyEvent.getExtendedKeyCodeForChar(ch);
                robot.delay(10);
                if (upperCase) {
                    robot.keyPress(KeyEvent.VK_SHIFT);
                }
                robot.keyPress(keyCode);
                robot.keyRelease(keyCode);
                if (upperCase) {
                    robot.keyRelease(KeyEvent.VK_SHIFT);
                }
            }

        } catch (final Exception e) {
            System.out.println(e);
        }
    }
}


来源:https://stackoverflow.com/questions/8875092/robot-class-java-typing-a-string-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!