Search code examples
javakeyboardawtrobot

Invalid key code @ java


I'm working on a system to type things automatically with java. This is how I write it:

public void typeMessage(String message) {
    for (char c : message.toCharArray()) {
        int code = c;
        if (code > 96 && code < 123) 
            code = code - 32;
        if (c == '@') {
            robot.keyPress(VK_SHIFT);
            robot.keyPress(VK_AT);
            robot.keyRelease(VK_SHIFT);
            robot.keyRelease(VK_AT);
        } else {
            type(code);
        }
    }
    type(VK_ENTER);
}

But I'm getting this error:

    Exception in thread "Thread-2" java.lang.IllegalArgumentException: Invalid key code

on

    robot.keyPress(VK_AT);

Solution

  • Your keyboard layout should have a key for the @ symbol for this code to work. Does it?

    If your keyboard is set up with an American layout you need to type shift+2 to type an @ symbol, and instead of VK_AT you have to use VK_2.