5

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);
}
eltabo
  • 3,749
  • 1
  • 21
  • 33
Bertrand
  • 341
  • 1
  • 2
  • 12
  • Are some of these characters outside the BMP? – fge Jan 16 '15 at 11:02
  • From [this link](http://www.fileformat.info/info/unicode/char/015f/index.htm), it appears that under Windows you should use Alt+015F. I don't have Windows so I can't test... – fge Jan 16 '15 at 11:06
  • doesn't work. Thanks – Bertrand Jan 16 '15 at 11:15
  • you can't press Alt+015F in Windows unless you have enabled `EnableHexNumpad` in registry http://www.fileformat.info/tip/microsoft/enter_unicode.htm http://en.wikipedia.org/wiki/Alt_code – phuclv Jan 16 '15 at 12:09
  • without HexNumpad only codepoints 1-255 can be entered – phuclv Jan 16 '15 at 12:25
  • possible duplicate of [How to make the Java.awt.Robot type unicode characters? (Is it possible?)](http://stackoverflow.com/questions/397113/how-to-make-the-java-awt-robot-type-unicode-characters-is-it-possible) – phuclv Jan 16 '15 at 12:26
  • Thanks for all, but I tested doesn't work, it may be necessary to use another class than class Robot – Bertrand Jan 16 '15 at 12:41
  • http://stackoverflow.com/questions/7279773/can-we-insert-unicode-characters-using-robot-class-in-java – phuclv Jan 16 '15 at 12:43
  • Did you try pressing the keys exactly like in the above links (`Alt`-`+HEX`)? Make sure to press `+` before the hexadecimal codepoint value, unlike decimal version – phuclv Jan 16 '15 at 12:46
  • Yes, I tested pressUnicode(new Robot(), 0x15F); it gives me the same result of my previous program output : _ – Bertrand Jan 16 '15 at 12:59
  • @user3210664 as I said, on Windows you must enable HexNumpad in registry, then press `Alt`+`+CODE`. That function simply presses decimal code, so it won't work in Windows – phuclv Feb 06 '15 at 06:24

3 Answers3

2

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).

Robert
  • 39,162
  • 17
  • 99
  • 152
1

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

Community
  • 1
  • 1
Ya Wang
  • 1,758
  • 1
  • 19
  • 41
0

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. :)

ozanonurtek
  • 306
  • 5
  • 18