0

I've seen similar questions about this issue, but what happens to me is a little different; I am developing a remote control application and I'm sending keystrokes to my computer. The Robot class in java only accepts VK_CODES for keystrokes, so I have to translate non ascii characters into keystrokes combinations, like this:

public void type(char character) {
        switch (character) {
        case 'a': doType(KeyEvent.VK_A); break;
        case 'á': doType(KeyEvent.VK_A); break;
        case 'à': doType(KeyEvent.VK_A); break;
        case 'ä': doType(KeyEvent.VK_A); break;
        case 'â': doType(KeyEvent.VK_A); break;
        case 'b': doType(KeyEvent.VK_B); break;
        case 'c': doType(KeyEvent.VK_C); break;
        case 'd': doType(KeyEvent.VK_D); break;
        case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break;
        case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break;
        case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break;

        etc...
    }

    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]);
    }

This works well, but when I try to combine ALT+numbers characters (for example to type the '@' character) I do:

case '@': doType(KeyEvent.VK_ALT,KeyEvent.VK_6,KeyEvent.VK_4); break;

It won't type it. If I type it directly with my keyboard, it works. Is there a reason for this? How can I make a Robot instance to accept all the unicode characters and not just ascii? Is there a better way to do what I am doing?

Thanks for reading and sorry for my English!

eltabo
  • 3,749
  • 1
  • 21
  • 33
Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53

1 Answers1

0

I found out that the problem was, I was entering the VK_number directly. Now it works like a charm using the VK_NUMPAD keys:

case '@': doType(KeyEvent.VK_ALT,KeyEvent.VK_NUMPAD6,KeyEvent.VK_NUMPAD4); break;
case '#': doType(KeyEvent.VK_ALT,KeyEvent.VK_NUMPAD3,KeyEvent.VK_NUMPAD5); break;

etc..

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53