16

我正在编写一个要求用户输入字符串的基本程序,并且我正在尝试使用一个机器人(来自java.awt.Robot),它会在延迟后将此消息输入到另一个文档中。我现在遇到的问题是我需要将我得到的任何内容转换message.charAt(i)KeyEvent.VK_[insert Char]KeyCode。有没有更好的方法来做我想做的事情?我想我总是可以有一个巨大的 switch 语句来获取适当的 KeyCode,但我希望会有一种更优雅的方式。我的第一个想法,做了一段时间的 python,是创建一个字符串"KeyEvent.VK_" + message.charAt(i)并以某种方式将其转换为代码,但我认为唯一的方法是使用不鼓励的反射。

4

4 回答 4

25

如果我有一个 unicode 字符并且我只想知道它的击键,那么我使用

int keyCode = java.awt.event.KeyEvent.getExtendedKeyCodeForChar(charCode);

我从来不需要另一种更复杂的方法来做到这一点。

于 2013-11-08T02:29:37.170 回答
12

你可以解决这个问题:

KeyStroke ks = KeyStroke.getKeyStroke('k', 0);
System.out.println(ks.getKeyCode());

或者只是使用这个:

private void writeString(String s) {
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (Character.isUpperCase(c)) {
            robot.keyPress(KeyEvent.VK_SHIFT);
        }
        robot.keyPress(Character.toUpperCase(c));
        robot.keyRelease(Character.toUpperCase(c));

        if (Character.isUpperCase(c)) {
            robot.keyRelease(KeyEvent.VK_SHIFT);
        }
    }
    robot.delay(delay);
}
于 2013-03-06T23:37:09.770 回答
2

更详细但更可定制

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

public class AsciiKeyTyper {

    private Map<Character,KeyStroke> strokeMap;
    private Robot robot;
    public AsciiKeyTyper() throws AWTException{
        robot=new Robot();
        //initialize a map from the input char to the keystroke,
        //using the fact that sometimes the KeyEvent key codes correspond to ASCII
        strokeMap=new HashMap<Character,KeyStroke>(){
                private static final long serialVersionUID = 1L;{
            put('\n',new KeyStroke(KeyEvent.VK_ENTER,false));
            put('\t',new KeyStroke(KeyEvent.VK_TAB,false));
            put('\r',new KeyStroke(KeyEvent.VK_HOME,false));
            put(' ',new KeyStroke(KeyEvent.VK_SPACE,false));
            put('!',new KeyStroke(KeyEvent.VK_1,true));
            put('"',new KeyStroke(KeyEvent.VK_QUOTE,true));
            put('#',new KeyStroke(KeyEvent.VK_3,true));
            put('$',new KeyStroke(KeyEvent.VK_4,true));
            put('%',new KeyStroke(KeyEvent.VK_5,true));
            put('&',new KeyStroke(KeyEvent.VK_7,true));
            put('\'',new KeyStroke(KeyEvent.VK_QUOTE,false));
            put('(',new KeyStroke(KeyEvent.VK_9,true));
            put(')',new KeyStroke(KeyEvent.VK_0,true));
            put('*',new KeyStroke(KeyEvent.VK_8,true));
            put('+',new KeyStroke(KeyEvent.VK_EQUALS,true));
            put(',',new KeyStroke(KeyEvent.VK_COMMA,false));
            put('-',new KeyStroke(KeyEvent.VK_MINUS,false));
            put('.',new KeyStroke(KeyEvent.VK_PERIOD,false));
            put('/',new KeyStroke(KeyEvent.VK_SLASH,false));
            for(int i=(int)'0';i<=(int)'9';i++){
                put((char)i,new KeyStroke(i,false));
            }
            put(':',new KeyStroke(KeyEvent.VK_SEMICOLON,true));
            put(';',new KeyStroke(KeyEvent.VK_SEMICOLON,false));
            put('<',new KeyStroke(KeyEvent.VK_COMMA,true));
            put('=',new KeyStroke(KeyEvent.VK_EQUALS,false));
            put('>',new KeyStroke(KeyEvent.VK_PERIOD,true));
            put('?',new KeyStroke(KeyEvent.VK_SLASH,true));
            put('@',new KeyStroke(KeyEvent.VK_2,true));
            for(int i=(int)'A';i<=(int)'Z';i++){
                put((char)i,new KeyStroke(i,true));
            }
            put('[',new KeyStroke(KeyEvent.VK_OPEN_BRACKET,false));
            put('\\',new KeyStroke(KeyEvent.VK_BACK_SLASH,false));
            put(']',new KeyStroke(KeyEvent.VK_CLOSE_BRACKET,false));
            put('^',new KeyStroke(KeyEvent.VK_6,true));
            put('_',new KeyStroke(KeyEvent.VK_MINUS,true));
            put('`',new KeyStroke(KeyEvent.VK_BACK_QUOTE,false));
            for(int i=(int)'A';i<=(int)'Z';i++){
                put((char)(i+((int)'a'-(int)'A')),new KeyStroke(i,false));
            }
            put('{',new KeyStroke(KeyEvent.VK_OPEN_BRACKET,true));
            put('|',new KeyStroke(KeyEvent.VK_BACK_SLASH,true));
            put('}',new KeyStroke(KeyEvent.VK_CLOSE_BRACKET,true));
            put('~',new KeyStroke(KeyEvent.VK_BACK_QUOTE,true));
        }};
    }
    public void typeKey(char key){
        try{
            strokeMap.get(key).type();
        }catch(NullPointerException ex){
            System.err.println("'"+key+"': no such key in mappings");
        }
    }
    private class KeyStroke{
        int code;
        boolean isShifted;
        public KeyStroke(int keyCode,boolean shift){
            code=keyCode;
            isShifted=shift;
        }
        public void type(){
            try{
                if (isShifted) {
                    robot.keyPress(KeyEvent.VK_SHIFT);
                }
                robot.keyPress(code);
                robot.keyRelease(code);
                if (isShifted) {
                    robot.keyRelease(KeyEvent.VK_SHIFT);
                }
                if(code==KeyEvent.VK_ENTER){
                    robot.keyPress(KeyEvent.VK_HOME);
                    robot.keyRelease(KeyEvent.VK_HOME);
                }

            }catch(IllegalArgumentException ex){
                String ch="";
                for(char key:strokeMap.keySet()){
                    if(strokeMap.get(key)==this){
                        ch=""+key;
                        break;
                    }
                }
                System.err.println("Key Code Not Recognized: '"+ch+"'->"+code);
            }
        }
    }
}
于 2017-05-01T16:50:36.013 回答
-1

使用反射!

KeyEvent.class.getField("VK_"+keyName);
于 2015-07-14T16:24:48.527 回答