Convertir une chaîne en KeyEvents

Je veux convertir une chaîne en KeyEvent pour faire quelque chose comme ceci:

writeKeyboard(myBot,"abcd");

public void writeKeyboard(Robot bot, String st){
     char[] arr = arr.toCharArray();
     int i = arr.length();
     int j = 0;
     int keycode;
     while (j<i) {
         keycode = arr[j].something;
         bot.keyPress(keycode);
         bot.keyRelease(keycode);
         j++;
     }
}
41
demandé sur Martin Trigaux 2009-08-08 14:21:03

16 réponses

J'utilise essentiellement une déclaration glorifiée switch. Simple et rapide:

import static java.awt.event.KeyEvent.*;

public class Keyboard {

    private Robot robot;

    public static void main(String... args) throws Exception {
        Keyboard keyboard = new Keyboard();
        keyboard.type("Hello there, how are you?");
    }

    public Keyboard() throws AWTException {
        this.robot = new Robot();
    }

    public Keyboard(Robot robot) {
        this.robot = robot;
    }

    public void type(CharSequence characters) {
        int length = characters.length();
        for (int i = 0; i < length; i++) {
            char character = characters.charAt(i);
            type(character);
        }
    }

    public void type(char character) {
        switch (character) {
        case 'a': doType(VK_A); break;
        case 'b': doType(VK_B); break;
        case 'c': doType(VK_C); break;
        case 'd': doType(VK_D); break;
        case 'e': doType(VK_E); break;
        case 'f': doType(VK_F); break;
        case 'g': doType(VK_G); break;
        case 'h': doType(VK_H); break;
        case 'i': doType(VK_I); break;
        case 'j': doType(VK_J); break;
        case 'k': doType(VK_K); break;
        case 'l': doType(VK_L); break;
        case 'm': doType(VK_M); break;
        case 'n': doType(VK_N); break;
        case 'o': doType(VK_O); break;
        case 'p': doType(VK_P); break;
        case 'q': doType(VK_Q); break;
        case 'r': doType(VK_R); break;
        case 's': doType(VK_S); break;
        case 't': doType(VK_T); break;
        case 'u': doType(VK_U); break;
        case 'v': doType(VK_V); break;
        case 'w': doType(VK_W); break;
        case 'x': doType(VK_X); break;
        case 'y': doType(VK_Y); break;
        case 'z': doType(VK_Z); break;
        case 'A': doType(VK_SHIFT, VK_A); break;
        case 'B': doType(VK_SHIFT, VK_B); break;
        case 'C': doType(VK_SHIFT, VK_C); break;
        case 'D': doType(VK_SHIFT, VK_D); break;
        case 'E': doType(VK_SHIFT, VK_E); break;
        case 'F': doType(VK_SHIFT, VK_F); break;
        case 'G': doType(VK_SHIFT, VK_G); break;
        case 'H': doType(VK_SHIFT, VK_H); break;
        case 'I': doType(VK_SHIFT, VK_I); break;
        case 'J': doType(VK_SHIFT, VK_J); break;
        case 'K': doType(VK_SHIFT, VK_K); break;
        case 'L': doType(VK_SHIFT, VK_L); break;
        case 'M': doType(VK_SHIFT, VK_M); break;
        case 'N': doType(VK_SHIFT, VK_N); break;
        case 'O': doType(VK_SHIFT, VK_O); break;
        case 'P': doType(VK_SHIFT, VK_P); break;
        case 'Q': doType(VK_SHIFT, VK_Q); break;
        case 'R': doType(VK_SHIFT, VK_R); break;
        case 'S': doType(VK_SHIFT, VK_S); break;
        case 'T': doType(VK_SHIFT, VK_T); break;
        case 'U': doType(VK_SHIFT, VK_U); break;
        case 'V': doType(VK_SHIFT, VK_V); break;
        case 'W': doType(VK_SHIFT, VK_W); break;
        case 'X': doType(VK_SHIFT, VK_X); break;
        case 'Y': doType(VK_SHIFT, VK_Y); break;
        case 'Z': doType(VK_SHIFT, VK_Z); break;
        case '`': doType(VK_BACK_QUOTE); break;
        case '0': doType(VK_0); break;
        case '1': doType(VK_1); break;
        case '2': doType(VK_2); break;
        case '3': doType(VK_3); break;
        case '4': doType(VK_4); break;
        case '5': doType(VK_5); break;
        case '6': doType(VK_6); break;
        case '7': doType(VK_7); break;
        case '8': doType(VK_8); break;
        case '9': doType(VK_9); break;
        case '-': doType(VK_MINUS); break;
        case '=': doType(VK_EQUALS); break;
        case '~': doType(VK_SHIFT, VK_BACK_QUOTE); break;
        case '!': doType(VK_EXCLAMATION_MARK); break;
        case '@': doType(VK_AT); break;
        case '#': doType(VK_NUMBER_SIGN); break;
        case '$': doType(VK_DOLLAR); break;
        case '%': doType(VK_SHIFT, VK_5); break;
        case '^': doType(VK_CIRCUMFLEX); break;
        case '&': doType(VK_AMPERSAND); break;
        case '*': doType(VK_ASTERISK); break;
        case '(': doType(VK_LEFT_PARENTHESIS); break;
        case ')': doType(VK_RIGHT_PARENTHESIS); break;
        case '_': doType(VK_UNDERSCORE); break;
        case '+': doType(VK_PLUS); break;
        case '\t': doType(VK_TAB); break;
        case '\n': doType(VK_ENTER); break;
        case '[': doType(VK_OPEN_BRACKET); break;
        case ']': doType(VK_CLOSE_BRACKET); break;
        case '\\': doType(VK_BACK_SLASH); break;
        case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break;
        case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break;
        case '|': doType(VK_SHIFT, VK_BACK_SLASH); break;
        case ';': doType(VK_SEMICOLON); break;
        case ':': doType(VK_COLON); break;
        case '\'': doType(VK_QUOTE); break;
        case '"': doType(VK_QUOTEDBL); break;
        case ',': doType(VK_COMMA); break;
        case '<': doType(VK_SHIFT, VK_COMMA); break;
        case '.': doType(VK_PERIOD); break;
        case '>': doType(VK_SHIFT, VK_PERIOD); break;
        case '/': doType(VK_SLASH); break;
        case '?': doType(VK_SHIFT, VK_SLASH); break;
        case ' ': doType(VK_SPACE); break;
        default:
            throw new IllegalArgumentException("Cannot type character " + character);
        }
    }

    private void doType(int... keyCodes) {
        doType(keyCodes, 0, keyCodes.length);
    }

    private void doType(int[] keyCodes, int offset, int length) {
        if (length == 0) {
            return;
        }

        robot.keyPress(keyCodes[offset]);
        doType(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
    }

}

Si vous voulez une saisie de clé personnalisée, vous pouvez étendre la classe et remplacer la méthode type(char). Par exemple:

import static java.awt.event.KeyEvent.*;

public class WindowUnicodeKeyboard extends Keyboard {

    private Robot robot;

    public WindowUnicodeKeyboard(Robot robot) {
        super(robot);
        this.robot = robot;
    }

    @Override
    public void type(char character) {
        try {
            super.type(character);
        } catch (IllegalArgumentException e) {
            String unicodeDigits = String.valueOf(Character.getCodePoint(character));
            robot.keyPress(VK_ALT);
            for (int i = 0; i < unicodeDigits.length(); i++) {
                typeNumPad(Integer.parseInt(unicodeDigits.substring(i, i + 1)));
            }
            robot.keyRelease(VK_ALT);
        }
    }

    private void typeNumPad(int digit) {
        switch (digit) {
        case 0: doType(VK_NUMPAD0); break;
        case 1: doType(VK_NUMPAD1); break;
        case 2: doType(VK_NUMPAD2); break;
        case 3: doType(VK_NUMPAD3); break;
        case 4: doType(VK_NUMPAD4); break;
        case 5: doType(VK_NUMPAD5); break;
        case 6: doType(VK_NUMPAD6); break;
        case 7: doType(VK_NUMPAD7); break;
        case 8: doType(VK_NUMPAD8); break;
        case 9: doType(VK_NUMPAD9); break;
        }
    }

}

Il y a, bien sûr, place à l'amélioration, mais vous avez l'idée.

77
répondu Adam Paynter 2014-11-11 13:47:43

J'ai utilisé le presse-papiers pour résoudre le problème...

public static void type(String characters) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection stringSelection = new StringSelection( characters );
    clipboard.setContents(stringSelection, clipboardOwner);

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
}
20
répondu Carl Bosch 2013-08-22 11:40:22

Vous pouvez le faire simplement via Java reflection API:

public void writeKeyboard(Robot bot, String st) {
    String upperCase = st.toUpperCase();

    for(int i = 0; i < upperCase.length(); i++) {
        String letter = Character.toString(upperCase.charAt(i));
        String code = "VK_" + letter

        Field f = KeyEvent.class.getField(code);
        int keyEvent = f.getInt(null);

        bot.press(keyEvent);
        bot.release(keyEvent);
    }
}
8
répondu Jindra Helcl 2015-01-22 11:53:33

J'ai récemment écrit une classe pour le faire. Il entre le code Alt de chaque caractère, au lieu de déterminer la combinaison de touches pour chaque cas individuellement. Pas la solution la plus rapide, mais elle est concise et fonctionne pour un très large éventail de caractères.

import java.awt.AWTException;
import java.awt.Robot;
import static java.awt.event.KeyEvent.VK_ALT;
import static java.awt.event.KeyEvent.VK_NUMPAD0;

public class Altbot extends Robot{

    Altbot()throws AWTException{}

    public void type(CharSequence cs){
        for(int i=0;i<cs.length();i++){
            type(cs.charAt(i));
        }
    }

    public void type(char c){
        keyPress(VK_ALT);
        keyPress(VK_NUMPAD0);
        keyRelease(VK_NUMPAD0);
        String altCode=Integer.toString(c);
        for(int i=0;i<altCode.length();i++){
            c=(char)(altCode.charAt(i)+'0');
            //delay(20);//may be needed for certain applications
            keyPress(c);
            //delay(20);//uncomment if necessary
            keyRelease(c);
        }
        keyRelease(VK_ALT);
    }
}
5
répondu Ryan Hilbert 2016-08-31 19:40:16

C'est ainsi que j'ai réussi à utiliser toutes les clés disponibles. Créez une carte et remplissez la carte en utilisant la méthode fillKeyMap. Vous pouvez maintenant utiliser la représentation en chaîne des événements clés et la convertir en codes KeyEvent à l'aide de la carte.

/** Create map with string values to integer pairs
 */
public static final void fillKeyMap(Map<String, Integer> into) {
    try {
        Field[] fields = KeyEvent.class.getDeclaredFields();
        for(Field f : fields) {
            if(f.getName().startsWith("VK_")) { //we only want these fields
                int code = ((Integer)f.get(null)).intValue();
                into.put(f.getName().substring(3), code);
            }
        }
    } catch(Exception ex) {}
}
4
répondu Dombrovski 2012-06-15 23:58:19

J'utilise essentiellement le modèle de commande comme le fait Rich Seller dans sa réponse, avec deux modifications mineures pour des raisons de brièveté:

  • utilisation du motif de décoration pour réutiliser les instances de la commande a-z
  • utilisation de reflection pour supprimer KeyEvent.VK_???

Interface de commande:

interface Command {

    void pressKey(Robot robot);
}

Et le décorateur (pour modification # 1):

class ShiftCommand implements Command {

    private final Command command;

    public ShiftCommand(Command command) {
        this.command = command;
    }

    public void pressKey(Robot robot) {
        robot.keyPress(KeyEvent.VK_SHIFT);
        command.pressKey(robot);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    }

    @Override
    public String toString() {
        return "SHIFT + " + command.toString();
    }
}

En utilisant cette aide (pour la modification # 2):

public static int getKeyEvent(Character c) {
    Field f = KeyEvent.class.getField("VK_" + Character.toUpperCase(c));
    f.setAccessible(true);
    return (Integer) f.get(null);
}

Attention : je suis ne pas gérer les exceptions ici, Ceci est laissé comme exercice pour vous:))

Puis remplir la commande en utilisant une boucle for:

Map<Character, Command> commandMap = new HashMap<Character, Command>();

    for (int i = 'a'; i <= 'z'; i++) {
        final Character c = Character.valueOf((char) i);
        Command pressKeyCommand = new Command() {

            public void pressKey(Robot robot) {
                int keyEventCode = getKeyEvent(c);
                robot.keyPress(c);
                robot.keyRelease(c);
            }

            @Override
            public String toString() {
                return String.format("%c", c);
            }
        };

        // 'a' .. 'z'
        commandMap.put(c, pressKeyCommand);
        // 'A' .. 'Z' by decorating pressKeyCommand
        commandMap.put(Character.toUpperCase(c), new ShiftCommand(pressKeyCommand));
    }

TestCase

String test = "aaaBBB";
for (int i = 0; i < test.length(); i++) {
     System.out.println(commandMap.get(test.charAt(i)));
}

Comme prévu, cette sortie:

a
a
a
SHIFT + b
SHIFT + b
SHIFT + b
3
répondu dfa 2009-08-08 11:55:06

C'est un peu un Kludge, mais vous pouvez utiliser le modèle de commande pour encapsuler les frappes pour chaque caractère de la chaîne, puis obtenir la commande pour chaque caractère à son tour et appeler la méthode. L'avantage, c'est que vous n'avez qu'à configurer la carte une fois. L'inconvénient est qu'il implique toujours un tas de bolierplate:

public interface Command {
    void pressKey(Robot bot);
}

//add a command to the map for each keystroke
commandMap.put("A", new Command() {
    void pressKey(Robot bot) {
        pressWithShift(KeyEvent.VK_A);
    }
});
commandMap.put ("a", new Command() {
    void pressKey (Robot bot) {
        press (KeyEvent.VK_A);
    }
});
commandMap.put("B", new Command() {
    void pressKey(Robot bot) {
        pressWithShift(KeyEvent.VK_B);
    }
});
...
//loads more definitions here

//helper methods
private void pressWithShift (Robot bot, KeyEvent event) {
    bot.keyPress (KeyEvent.VK_SHIFT);
    press(bot, event);
    bot.keyRelase(KeyEvent.VK_SHIFT);
}

private void press(Robot bot, KeyEvent event) {
    bot.keyPress(event);
    bot.keyRelease(event);
}

Ensuite, pour utiliser la carte:

for (int i = 0; i < st.length(); i++) {
    String subString = st.substring(i, i + 1);

    commandMap.get(subString).pressKey(bot);
}
3
répondu Rich Seller 2013-09-30 10:48:59

Votre code fonctionnera tant que vous n'utilisez que des caractères alphanumériques, cela ne fonctionnera pas pour des caractères comme":". SmartRobot classe va gérer cela.

2
répondu Demiurg 2009-09-20 22:46:28

La réponse acceptée me semble très gonflée. Voici une solution un peu plus concise qui est en quelque sorte basée sur la réponse acceptée. Il devrait gérer à peu près n'importe quel cas d'utilisation. Gère également \n (nouvelle ligne), \t (tab) et \b (Retour arrière), comme démontré dans l'exemple.

import java.awt.AWTException;
import java.awt.AWTKeyStroke;
import static java.awt.AWTKeyStroke.getAWTKeyStroke;
import java.awt.Robot;
import static java.awt.event.InputEvent.SHIFT_DOWN_MASK;
import java.awt.event.KeyEvent;

public class Keyboard {

    private final Robot robot;

    public Keyboard() throws AWTException {
        this.robot = new Robot();
    }

    public static void main(String[] args) throws Exception {
        Runtime.getRuntime().exec("notepad.exe");
        Thread.sleep(3000L);
        Keyboard keyboard = new Keyboard();
        keyboard.type("`1234567890-=[]\\;',./\n");
        keyboard.type("~!@#$%^&*()_+{}|:\"<>?\n");
        keyboard.type("abcdefghijklmnopqrstuvwxyz\n\tABCDEFGHIJKLMNOPQRSTUVWXYZ");
        keyboard.type("\n\n\twh\bat");
    }

    private static AWTKeyStroke getKeyStroke(char c) {
        String upper = "`~'\"!@#$%^&*()_+{}|:<>?";
        String lower = "`~'\"1234567890-=[]\\;,./";

        int index = upper.indexOf(c);
        if (index != -1) {
            int keyCode;
            boolean shift = false;
            switch (c) {
                // these chars need to be handled specially because
                // they don't directly translate into the correct keycode
                case '~':
                    shift = true;
                case '`':
                    keyCode = KeyEvent.VK_BACK_QUOTE;
                    break;
                case '\"':
                    shift = true;
                case '\'':
                    keyCode = KeyEvent.VK_QUOTE;
                    break;
                default:
                    keyCode = (int) Character.toUpperCase(lower.charAt(index));
                    shift = true;
            }
            return getAWTKeyStroke(keyCode, shift ? SHIFT_DOWN_MASK : 0);
        }
        return getAWTKeyStroke((int) Character.toUpperCase(c), 0);
    }

    public void type(CharSequence chars) {
        type(chars, 0);
    }

    public void type(CharSequence chars, int ms) {
        ms = ms > 0 ? ms : 0;
        for (int i = 0, len = chars.length(); i < len; i++) {
            char c = chars.charAt(i);
            AWTKeyStroke keyStroke = getKeyStroke(c);
            int keyCode = keyStroke.getKeyCode();
            boolean shift = Character.isUpperCase(c) || keyStroke.getModifiers() == (SHIFT_DOWN_MASK + 1);
            if (shift) {
                robot.keyPress(KeyEvent.VK_SHIFT);
            }

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

            if (shift) {
                robot.keyRelease(KeyEvent.VK_SHIFT);
            }
            if (ms > 0) {
                robot.delay(ms);
            }
        }
    }
}
2
répondu Sensei_Shoh 2016-05-12 07:03:05
import java.awt.Robot;
import java.awt.event.KeyEvent;

/**
 * @author Trevor
 * Handles alpha numerics and common punctuation
 */
public class RobotKeyboard{

    private Robot robot;

    public RobotKeyboard(Robot robot){
        this.robot = robot;
    }

    public void typeMessage(String message){
        for (int i = 0; i < message.length(); i++){
            handleRepeatCharacter(message, i);
            type(message.charAt(i));
        }
    }

    private void handleRepeatCharacter(String message, int i){
        if(i == 0)
            return;
        //The robot won't type the same letter twice unless we release a key.
        if(message.charAt(i) == message.charAt(i-1)){
            robot.keyPress(KeyEvent.VK_SHIFT);
            robot.keyRelease(KeyEvent.VK_SHIFT);
        }
    }

    private void type(char character){
        handleSpecialCharacter(character);

        if (Character.isLowerCase(character)){
            typeCharacter(Character.toUpperCase(character));
        }
        if (Character.isUpperCase(character)){
            typeShiftCharacter(character);
        }
        if (Character.isDigit(character)){
            typeCharacter(character);
        }
    }

    private void handleSpecialCharacter(char character){
        if (character == ' ')
            typeCharacter(KeyEvent.VK_SPACE);
        if (character == '.')
            typeCharacter(KeyEvent.VK_PERIOD);
        if (character == '!')
            typeShiftCharacter(KeyEvent.VK_1);
        if (character == '?')
            typeShiftCharacter(KeyEvent.VK_SLASH);
        if (character == ',')
            typeCharacter(KeyEvent.VK_COMMA);

        //More specials here as needed
    }

    private void typeCharacter(int character){
        robot.keyPress(character);
    }

    private void typeShiftCharacter(int character){
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(character);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    }

}
1
répondu trevorism 2011-10-11 21:49:45

J'ai eu le même problème. J'ai trouvé que le code ci-dessous était un moyen simple de traduire une chaîne en événements clés. Il semble que la plupart des gens avaient des correctifs très spécifiques à l'implémentation pour tenir compte des différences entre les minuscules et les majuscules. Parce que les clés VK ne se soucient pas des majuscules/ minuscules, tout casting en majuscules semble résoudre le problème.

for(int i = 0; i < string.length(); i++){
    char c = Character.toUpperCase(s.charAt(i));
    KeyEvent ke = new KeyEvent(source, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, (int)c, c);
    //do whatever with key event
}

Je voulais aussi dire que cela fonctionne bien pour les lettres et les chiffres, mais cela peut ne pas fonctionner pour les autres caractères. Cette technique traduit la marque accent / backtick dans le pavé numérique 0 (VK_NUMPAD0)

1
répondu Gregory Peck 2013-05-14 14:39:38

J'aimerais avoir 50 points de réputation à commenter Adam Paynter solution, mais pas encore....

Sa classe Keyboard a bien fonctionné, mais j'ai eu des problèmes avec des clés qui ne sont pas alphabétiques, telles que :, \ et /.

La solution que j'ai trouvée était d'appuyer et de maintenir <ALT> pendant que <ASCII code> de ces touches sont tapées.

Tout d'abord, j'ai changé la méthode doType() pour ne tenir que SHIFT et/ou ALTenfoncés, mais pas toutes les touches qui composent le code ASCII. Joli beaucoup, c'est ainsi que les humains utilisent les claviers:)

private void doType(int[] keyCodes, int offset, int length) {
    if (length == 0) {
        return;
    }

    robot.keyPress(keyCodes[offset]);

    if (keyCodes[offset] == KeyEvent.VK_ALT
        || keyCodes[offset] == KeyEvent.VK_SHIFT) {
        doType(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
    } else {
        robot.keyRelease(keyCodes[offset]);
        doType(keyCodes, offset + 1, length - 1);
    }

}

Ensuite, j'ai changé le cas de commutateur {[15] } pour passer ALT et le code ASCII:

        case '\\' :
            doType(KeyEvent.VK_ALT, KeyEvent.VK_NUMPAD9,
                    KeyEvent.VK_NUMPAD2);
        case ':' :
            doType(KeyEvent.VK_ALT, KeyEvent.VK_NUMPAD5,
                    KeyEvent.VK_NUMPAD8);
        case '/' :
            doType(KeyEvent.VK_ALT, KeyEvent.VK_NUMPAD4,
                    KeyEvent.VK_NUMPAD7);

======================================================= ADDITION

J'ai peaufiné ce cours un peu plus:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Keyboard {

    private static final Logger LOG = LoggerFactory.getLogger(Utils.class);

    private Robot robot;

    public Keyboard() {
        try {
            this.robot = new Robot();
        } catch (AWTException e) {
            LOG.error("Unable to create Robot object", e);
            throw new RuntimeException(e);
        }
    }

    public void type(String text) {
        char c;

        for (int ii = 0; ii < text.length(); ii++) {
            c = text.charAt(ii);

            if (c <= 31 || c == 129) {
                pressControlKey(c);
            } else {
                typeAsciiCode(c);
            }
        }
    }

    private void pressControlKey(char c) {
        robot.keyPress(c);
        robot.keyRelease(c);
    }

    private void typeAsciiCode(char c) {
        robot.keyPress(KeyEvent.VK_ALT);

        String asciiCode = Integer.toString(c);
        for (int i = 0; i < asciiCode.length(); i++) {
            c = (char) (asciiCode.charAt(i) + '0');
            robot.keyPress(c);
            robot.keyRelease(c);
        }
        robot.keyRelease(KeyEvent.VK_ALT);
    }
}
1
répondu cbaldan 2016-04-18 14:13:15

Légère modification de la réponse D'Adam Paynter car le code ne fonctionnait pas pour toutes les clés, par exemple !, @,#, $ etc. (Nécessaire pour appuyer sur VK_SHIFT en premier)

public void type(char character) {
    switch (character) {
    case 'a': doType(VK_A); break;
    case 'b': doType(VK_B); break;
    case 'c': doType(VK_C); break;
    case 'd': doType(VK_D); break;
    case 'e': doType(VK_E); break;
    case 'f': doType(VK_F); break;
    case 'g': doType(VK_G); break;
    case 'h': doType(VK_H); break;
    case 'i': doType(VK_I); break;
    case 'j': doType(VK_J); break;
    case 'k': doType(VK_K); break;
    case 'l': doType(VK_L); break;
    case 'm': doType(VK_M); break;
    case 'n': doType(VK_N); break;
    case 'o': doType(VK_O); break;
    case 'p': doType(VK_P); break;
    case 'q': doType(VK_Q); break;
    case 'r': doType(VK_R); break;
    case 's': doType(VK_S); break;
    case 't': doType(VK_T); break;
    case 'u': doType(VK_U); break;
    case 'v': doType(VK_V); break;
    case 'w': doType(VK_W); break;
    case 'x': doType(VK_X); break;
    case 'y': doType(VK_Y); break;
    case 'z': doType(VK_Z); break;
    case 'A': doType(VK_SHIFT, VK_A); break;
    case 'B': doType(VK_SHIFT, VK_B); break;
    case 'C': doType(VK_SHIFT, VK_C); break;
    case 'D': doType(VK_SHIFT, VK_D); break;
    case 'E': doType(VK_SHIFT, VK_E); break;
    case 'F': doType(VK_SHIFT, VK_F); break;
    case 'G': doType(VK_SHIFT, VK_G); break;
    case 'H': doType(VK_SHIFT, VK_H); break;
    case 'I': doType(VK_SHIFT, VK_I); break;
    case 'J': doType(VK_SHIFT, VK_J); break;
    case 'K': doType(VK_SHIFT, VK_K); break;
    case 'L': doType(VK_SHIFT, VK_L); break;
    case 'M': doType(VK_SHIFT, VK_M); break;
    case 'N': doType(VK_SHIFT, VK_N); break;
    case 'O': doType(VK_SHIFT, VK_O); break;
    case 'P': doType(VK_SHIFT, VK_P); break;
    case 'Q': doType(VK_SHIFT, VK_Q); break;
    case 'R': doType(VK_SHIFT, VK_R); break;
    case 'S': doType(VK_SHIFT, VK_S); break;
    case 'T': doType(VK_SHIFT, VK_T); break;
    case 'U': doType(VK_SHIFT, VK_U); break;
    case 'V': doType(VK_SHIFT, VK_V); break;
    case 'W': doType(VK_SHIFT, VK_W); break;
    case 'X': doType(VK_SHIFT, VK_X); break;
    case 'Y': doType(VK_SHIFT, VK_Y); break;
    case 'Z': doType(VK_SHIFT, VK_Z); break;
    case '`': doType(VK_BACK_QUOTE); break;
    case '0': doType(VK_0); break;
    case '1': doType(VK_1); break;
    case '2': doType(VK_2); break;
    case '3': doType(VK_3); break;
    case '4': doType(VK_4); break;
    case '5': doType(VK_5); break;
    case '6': doType(VK_6); break;
    case '7': doType(VK_7); break;
    case '8': doType(VK_8); break;
    case '9': doType(VK_9); break;
    case '-': doType(VK_MINUS); break;
    case '=': doType(VK_EQUALS); break;
    case '~': doType(VK_BACK_QUOTE); break;
    case '!': doType(VK_SHIFT, VK_EXCLAMATION_MARK); break;
    case '@': doType(VK_SHIFT, VK_AT); break;
    case '#': doType(VK_SHIFT, VK_NUMBER_SIGN); break;
    case '$': doType(VK_SHIFT, VK_DOLLAR); break;
    case '%': doType(VK_SHIFT, VK_5); break;
    case '^': doType(VK_SHIFT, VK_CIRCUMFLEX); break;
    case '&': doType(VK_SHIFT, VK_AMPERSAND); break;
    case '*': doType(VK_SHIFT, VK_ASTERISK); break;
    case '(': doType(VK_LEFT_PARENTHESIS); break;
    case ')': doType(VK_RIGHT_PARENTHESIS); break;
    case '_': doType(VK_SHIFT, VK_UNDERSCORE); break;
    case '+': doType(VK_SHIFT, VK_PLUS); break;
    case '\t': doType(VK_TAB); break;
    case '\n': doType(VK_ENTER); break;
    case '[': doType(VK_OPEN_BRACKET); break;
    case ']': doType(VK_CLOSE_BRACKET); break;
    case '\\': doType(VK_BACK_SLASH); break;
    case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break;
    case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break;
    case '|': doType(VK_SHIFT, VK_BACK_SLASH); break;
    case ';': doType(VK_SEMICOLON); break;
    case ':': doType(VK_SHIFT, VK_COLON); break;
    case '\'': doType(VK_QUOTE); break;
    case '"': doType(VK_SHIFT, VK_QUOTEDBL); break;
    case ',': doType(VK_COMMA); break;
    case '<': doType(VK_SHIFT, VK_COMMA); break;
    case '.': doType(VK_PERIOD); break;
    case '>': doType(VK_SHIFT, VK_PERIOD); break;
    case '/': doType(VK_SLASH); break;
    case '?': doType(VK_SHIFT, VK_SLASH); break;
    case ' ': doType(VK_SPACE); break;
    case '\b': doType(VK_BACK_SPACE); break;
    default:
        throw new IllegalArgumentException("Cannot type character " + character);
    }
}
1
répondu Varun Kumar 2016-10-17 06:19:44

Je ne connais pas la méthode commandmap mais ça a l'air bien, je vais jeter un oeil.

Enfin, je découvre que le keycode de a à z est de 65 à 90. Donc

private void write(Robot bot, String st) {
    char[] arr = st.toCharArray();
    int i = arr.length;
    int j = 0;
    while (j<i) {
        int kcode = (int) arr[j] - 32;
        bot.keyPress(kcode);
        bot.keyRelease(kcode);
        j++;
    }       
}

Cela fonctionne juste pour les lettres minuscules (vous pouvez facilement le corriger avec un simple test pour les majuscules).

Pour ce que je cherchais, cela fonctionne parfaitement: -)

0
répondu Martin Trigaux 2009-08-08 12:31:18

J'ai trouvé que la combinaison des réponses de Ryan Hilbert et Adam Paynter m'a donné le meilleur résultat. Il combine la vitesse des touches simples avec la robustesse des codes alt. Le code combiné:

/**
 * Created by Daniel on 25/8/2016.
 */

import java.awt.AWTException;
import java.awt.Robot;

import static java.awt.event.KeyEvent.VK_0;
import static java.awt.event.KeyEvent.VK_1;
import static java.awt.event.KeyEvent.VK_2;
import static java.awt.event.KeyEvent.VK_3;
import static java.awt.event.KeyEvent.VK_4;
import static java.awt.event.KeyEvent.VK_5;
import static java.awt.event.KeyEvent.VK_6;
import static java.awt.event.KeyEvent.VK_7;
import static java.awt.event.KeyEvent.VK_8;
import static java.awt.event.KeyEvent.VK_9;
import static java.awt.event.KeyEvent.VK_A;
import static java.awt.event.KeyEvent.VK_ALT;
import static java.awt.event.KeyEvent.VK_B;
import static java.awt.event.KeyEvent.VK_BACK_QUOTE;
import static java.awt.event.KeyEvent.VK_BACK_SLASH;
import static java.awt.event.KeyEvent.VK_C;
import static java.awt.event.KeyEvent.VK_CLOSE_BRACKET;
import static java.awt.event.KeyEvent.VK_COLON;
import static java.awt.event.KeyEvent.VK_COMMA;
import static java.awt.event.KeyEvent.VK_D;
import static java.awt.event.KeyEvent.VK_E;
import static java.awt.event.KeyEvent.VK_ENTER;
import static java.awt.event.KeyEvent.VK_EQUALS;
import static java.awt.event.KeyEvent.VK_F;
import static java.awt.event.KeyEvent.VK_G;
import static java.awt.event.KeyEvent.VK_H;
import static java.awt.event.KeyEvent.VK_I;
import static java.awt.event.KeyEvent.VK_J;
import static java.awt.event.KeyEvent.VK_K;
import static java.awt.event.KeyEvent.VK_L;
import static java.awt.event.KeyEvent.VK_M;
import static java.awt.event.KeyEvent.VK_MINUS;
import static java.awt.event.KeyEvent.VK_N;
import static java.awt.event.KeyEvent.VK_NUMPAD0;
import static java.awt.event.KeyEvent.VK_O;
import static java.awt.event.KeyEvent.VK_OPEN_BRACKET;
import static java.awt.event.KeyEvent.VK_P;
import static java.awt.event.KeyEvent.VK_PERIOD;
import static java.awt.event.KeyEvent.VK_Q;
import static java.awt.event.KeyEvent.VK_QUOTE;
import static java.awt.event.KeyEvent.VK_QUOTEDBL;
import static java.awt.event.KeyEvent.VK_R;
import static java.awt.event.KeyEvent.VK_S;
import static java.awt.event.KeyEvent.VK_SEMICOLON;
import static java.awt.event.KeyEvent.VK_SHIFT;
import static java.awt.event.KeyEvent.VK_SLASH;
import static java.awt.event.KeyEvent.VK_SPACE;
import static java.awt.event.KeyEvent.VK_T;
import static java.awt.event.KeyEvent.VK_TAB;
import static java.awt.event.KeyEvent.VK_U;
import static java.awt.event.KeyEvent.VK_V;
import static java.awt.event.KeyEvent.VK_W;
import static java.awt.event.KeyEvent.VK_X;
import static java.awt.event.KeyEvent.VK_Y;
import static java.awt.event.KeyEvent.VK_Z;

public class Keyboard { // http://stackoverflow.com/a/1248709/4330555

    private Robot robot;

    public Keyboard() throws AWTException {
        this.robot = new Robot();
    }

    public Keyboard(Robot robot) {
        this.robot = robot;
    }

    public void type(CharSequence characters) {
        int length = characters.length();
        for (int i = 0; i < length; i++) {
            char character = characters.charAt(i);
            try {
                type(character);
            } catch (IllegalArgumentException e) {
                robot.keyPress(VK_ALT);
                robot.keyPress(VK_NUMPAD0);
                robot.keyRelease(VK_NUMPAD0);
                String altCode = Integer.toString(character);
                for (int j = 0; j < altCode.length(); j++) {
                    char c = (char) (altCode.charAt(j) + '0');
                    robot.keyPress(c);
                    robot.keyRelease(c);
                }
                robot.keyRelease(VK_ALT);
            }
        }
    }

    public void type(char character) {
        switch (character) {
            case 'a':
                doType(VK_A);
                break;
            case 'b':
                doType(VK_B);
                break;
            case 'c':
                doType(VK_C);
                break;
            case 'd':
                doType(VK_D);
                break;
            case 'e':
                doType(VK_E);
                break;
            case 'f':
                doType(VK_F);
                break;
            case 'g':
                doType(VK_G);
                break;
            case 'h':
                doType(VK_H);
                break;
            case 'i':
                doType(VK_I);
                break;
            case 'j':
                doType(VK_J);
                break;
            case 'k':
                doType(VK_K);
                break;
            case 'l':
                doType(VK_L);
                break;
            case 'm':
                doType(VK_M);
                break;
            case 'n':
                doType(VK_N);
                break;
            case 'o':
                doType(VK_O);
                break;
            case 'p':
                doType(VK_P);
                break;
            case 'q':
                doType(VK_Q);
                break;
            case 'r':
                doType(VK_R);
                break;
            case 's':
                doType(VK_S);
                break;
            case 't':
                doType(VK_T);
                break;
            case 'u':
                doType(VK_U);
                break;
            case 'v':
                doType(VK_V);
                break;
            case 'w':
                doType(VK_W);
                break;
            case 'x':
                doType(VK_X);
                break;
            case 'y':
                doType(VK_Y);
                break;
            case 'z':
                doType(VK_Z);
                break;
            case 'A':
                doType(VK_SHIFT, VK_A);
                break;
            case 'B':
                doType(VK_SHIFT, VK_B);
                break;
            case 'C':
                doType(VK_SHIFT, VK_C);
                break;
            case 'D':
                doType(VK_SHIFT, VK_D);
                break;
            case 'E':
                doType(VK_SHIFT, VK_E);
                break;
            case 'F':
                doType(VK_SHIFT, VK_F);
                break;
            case 'G':
                doType(VK_SHIFT, VK_G);
                break;
            case 'H':
                doType(VK_SHIFT, VK_H);
                break;
            case 'I':
                doType(VK_SHIFT, VK_I);
                break;
            case 'J':
                doType(VK_SHIFT, VK_J);
                break;
            case 'K':
                doType(VK_SHIFT, VK_K);
                break;
            case 'L':
                doType(VK_SHIFT, VK_L);
                break;
            case 'M':
                doType(VK_SHIFT, VK_M);
                break;
            case 'N':
                doType(VK_SHIFT, VK_N);
                break;
            case 'O':
                doType(VK_SHIFT, VK_O);
                break;
            case 'P':
                doType(VK_SHIFT, VK_P);
                break;
            case 'Q':
                doType(VK_SHIFT, VK_Q);
                break;
            case 'R':
                doType(VK_SHIFT, VK_R);
                break;
            case 'S':
                doType(VK_SHIFT, VK_S);
                break;
            case 'T':
                doType(VK_SHIFT, VK_T);
                break;
            case 'U':
                doType(VK_SHIFT, VK_U);
                break;
            case 'V':
                doType(VK_SHIFT, VK_V);
                break;
            case 'W':
                doType(VK_SHIFT, VK_W);
                break;
            case 'X':
                doType(VK_SHIFT, VK_X);
                break;
            case 'Y':
                doType(VK_SHIFT, VK_Y);
                break;
            case 'Z':
                doType(VK_SHIFT, VK_Z);
                break;
            case '`':
                doType(VK_BACK_QUOTE);
                break;
            case '0':
                doType(VK_0);
                break;
            case '1':
                doType(VK_1);
                break;
            case '2':
                doType(VK_2);
                break;
            case '3':
                doType(VK_3);
                break;
            case '4':
                doType(VK_4);
                break;
            case '5':
                doType(VK_5);
                break;
            case '6':
                doType(VK_6);
                break;
            case '7':
                doType(VK_7);
                break;
            case '8':
                doType(VK_8);
                break;
            case '9':
                doType(VK_9);
                break;
            case '-':
                doType(VK_MINUS);
                break;
            case '=':
                doType(VK_EQUALS);
                break;
            case '~':
                doType(VK_SHIFT, VK_BACK_QUOTE);
                break;
            case '!':
                doType(VK_SHIFT, VK_1);
                break;
            case '@':
                doType(VK_SHIFT, VK_2);
                break;
            case '#':
                doType(VK_SHIFT, VK_3);
                break;
            case '$':
                doType(VK_SHIFT, VK_4);
                break;
            case '%':
                doType(VK_SHIFT, VK_5);
                break;
            case '^':
                doType(VK_SHIFT, VK_6);
                break;
            case '&':
                doType(VK_SHIFT, VK_7);
                break;
            case '*':
                doType(VK_SHIFT, VK_8);
                break;
            case '(':
                doType(VK_SHIFT, VK_9);
                break;
            case ')':
                doType(VK_SHIFT, VK_0);
                break;
            case '_':
                doType(VK_SHIFT, VK_MINUS);
                break;
            case '+':
                doType(VK_SHIFT, VK_EQUALS);
                break;
            case '\t':
                doType(VK_TAB);
                break;
            case '\n':
                doType(VK_ENTER);
                break;
            case '[':
                doType(VK_OPEN_BRACKET);
                break;
            case ']':
                doType(VK_CLOSE_BRACKET);
                break;
            case '\\':
                doType(VK_BACK_SLASH);
                break;
            case '{':
                doType(VK_SHIFT, VK_OPEN_BRACKET);
                break;
            case '}':
                doType(VK_SHIFT, VK_CLOSE_BRACKET);
                break;
            case '|':
                doType(VK_SHIFT, VK_BACK_SLASH);
                break;
            case ';':
                doType(VK_SEMICOLON);
                break;
            case ':':
                doType(VK_COLON);
                break;
            case '\'':
                doType(VK_QUOTE);
                break;
            case '"':
                doType(VK_QUOTEDBL);
                break;
            case ',':
                doType(VK_COMMA);
                break;
            case '<':
                doType(VK_SHIFT, VK_COMMA);
                break;
            case '.':
                doType(VK_PERIOD);
                break;
            case '>':
                doType(VK_SHIFT, VK_PERIOD);
                break;
            case '/':
                doType(VK_SLASH);
                break;
            case '?':
                doType(VK_SHIFT, VK_SLASH);
                break;
            case ' ':
                doType(VK_SPACE);
                break;
            default:
                throw new IllegalArgumentException("Cannot type character " + character);
        }
    }

    private void doType(int... keyCodes) {
        doType(keyCodes, 0, keyCodes.length);
    }

    private void doType(int[] keyCodes, int offset, int length) {
        if (length == 0) {
            return;
        }

        robot.keyPress(keyCodes[offset]);
        doType(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
    }

}
0
répondu Daniël van den Berg 2016-08-25 09:44:34

Juste un autre exemple. Peut devenir pratique.

public void writeKeyboard(Robot robot, String string)  {
    int ascii;
    for(int i = 0; i < string.length(); i++) {
        ascii = (int)string.charAt(i);
        robot.keyPress(ascii);
        robot.delay(10);// choose your time or random it
        robot.keyRelease(ascii);
        robot.delay(10);
    }
}
0
répondu Nelson Macieira 2017-07-19 00:10:57