Java Robot class press Turkish letter (Ö, ö, Ş, ş,

2019-02-17 17:33发布

I have problem with press a special letter (Turkish etc.) via java robot class. I hava a method to press keys which works as alt+keycode. I cant convert some special letters to current keycode. So how can I solve it. Thanx

For Example:

KeyStroke ks = KeyStroke.getKeyStroke('ö', 0);
 System.out.println(ks.getKeyCode());
 Output : 246
 // So alt+0246='ö'
 //but if I convert 'ş' to keycode
 //Output is 351 . So alt+351= '_' and alt+0351= '_' 
 //What is the Correct combination for 'ş'. same for 'Ş', 'ş','Ğ', 'ğ', 'İ', 'ı', 'Ə', 'ə'

KeyPress:

public void altNumpad(int... numpadCodes) {
    if (numpadCodes.length == 0) {
        return;
    }

    robot.keyPress(VK_ALT);

    for (int NUMPAD_KEY : numpadCodes) {
        robot.keyPress(NUMPAD_KEY);
        robot.keyRelease(NUMPAD_KEY);
    }

    robot.keyRelease(VK_ALT);
}

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-17 18:12

I know this is a late answer but here it is how I handle this problem for Turkish QWERTY keyboard

   static void writeRobotWrite(Robot robot, String keys) throws InterruptedException {
    ....
        try {
            robot.keyPress(keyCode);
            robot.delay(20);
            robot.keyRelease(keyCode);
            robot.delay(20);
        }catch (IllegalArgumentException e)
        {
            pressUnicode(c, robot);
        }

    }
}

Basically when I got undefined keyCode for Robot I call pressUnicode function which is:

    static void pressUnicode(char c, Robot robot)
{
    String cantRecognize = ""+c;
    StringSelection selection = new StringSelection(cantRecognize);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(selection, null);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
}

Simply I'm just copying and pasting the character. This is working for all undefined characters. :)

查看更多
我只想做你的唯一
3楼-- · 2019-02-17 18:13

The character numbers are defiunied in the Unicode standard. The are also used in HTML, therefore you can use this table.

Anyway if you see the character in the source code depends on the fact that the editor interprets the file correctly (UTF-8 is preferred).

Second the used editor must have a font installed that contains these characters. Hence if you type alt+0351 and get and '_' this may just be a replacement character indicating that the font misses this character.

And in the end you should tell the Java compiler that the source code is UTF-8 - just to make sure (javac -encoding utf8).

查看更多
疯言疯语
4楼-- · 2019-02-17 18:14

I am not sure why you did

KeyStroke ks = KeyStroke.getKeyStroke('ö', 0);

Because java docs say,

public static KeyStroke getKeyStroke(Character keyChar,
                 int modifiers)
//Use 0 to specify no modifiers.

you need to pass a modifier other than 0 to the overload.

You should try to pass a modification like,

java.awt.event.InputEvent.ALT_DOWN_MASK

So probably should try,

KeyStroke ks = KeyStroke.getKeyStroke('ö', java.awt.event.InputEvent.ALT_DOWN_MASK);

Java doc as reference: http://docs.oracle.com/javase/7/docs/api/javax/swing/KeyStroke.html#getKeyStroke(char)

If you cannot properly get a output from that then you should consider the fact the character is UTF-8 This might help you in that regard, Java, Using Scanner to input characters as UTF-8, can't print text

查看更多
登录 后发表回答