javakeyboardshiftqwerty

Java Keyboard get shifted char


I've made a key listener for my app and I need to identify the key code when the user presses the shift key + a key.

For example, when the user presses the shift key + the key 2 (English keyboard), it should print @. I made a quick switch/case to identify which is the shifted key, but it doesn't work on azerty keyboards nor mac ones.

Is there a way in java to get the shifted key or something?

It's quite hard to explain.

My java app just get the keycode, what i need is the shifted keycode associated to a keycode.

ex:

For the moment, my code is like that (Only works for qwerty KB)

String value = String.valueOf((char)key);
    if (shift)
    {
        switch (value.charAt(0))
        {
            case '1':
                return "!";
            case '2':
                return "@";
            case '3':
                return "#";

            .....

Thanks for your help.

Regards.


Solution

  • Finally, I made a frame that allows user to select his keyboard layout. Then I just made a simple swith/case to get the shifted key.

    String value = String.valueOf((char)key);
    if (shift && keyboard.equals("QWERTY"))
    {
        switch (value.charAt(0))
        {
            case '1':
                return "!";
            case '2':
                return "@";
            case '3':
                return "#";
        }
    }
    else if (shift && keyboard.equals("AZERTY"))
    {
        switch (value.charAt(0))
        {
            case '&':
                return "1";
            case 'é':
                return "2";
            case '"':
                return "3";
        }
    }
    else return value;
    

    I hope this could help someone else.

    Regards.