Search code examples
javalinuxswingawtrobot

JAVA Robot to type Norwegian characters


I want to type letters like å,ø,æ using java awt robot. but unable to find any keyevents for these letters. I am using ubuntu and Norwegian is selected as a language. so i can type these letters from keyboard but when i try to get keycode these are always given as 0.

Can someone suggest how i can write using java robot.


Solution

  • based on the solution of Wolfgang Steiner:

    public static void pressUnicode(Robot r, int key_code)
    {
        r.keyPress(KeyEvent.VK_ALT);
    
        for(int i = 3; i >= 0; --i)
        {
            // extracts a single decade of the key-code and adds
            // an offset to get the required VK_NUMPAD key-code
            int numpad_kc = key_code / (int) (Math.pow(10, i)) % 10 + KeyEvent.VK_NUMPAD0;
    
            r.keyPress(numpad_kc);
            r.keyRelease(numpad_kc);
        }
    
        r.keyRelease(KeyEvent.VK_ALT);
    }
    

    This automatically goes through each decade of the unicode key-code, maps it to the corresponding VK_NUMPAD equivalent and presses/releases the keys accordingly. It works with every unicode Character.

    To get the code of a Symbol do the following:

    char ch='ä';
    int key_code = ch;
    System.out.println(key_code);
    

    Edit: This solution is windows-based. To make it work for Linux try the following:

    "For linux - Hold down 'ctrl' & 'shift' the entire time while entering the character. While holding them down, press 'U' then the 4 digit octal code for the character. "

    in detail: https://pthree.org/2006/11/30/its-unicode-baby/