Java Keyboard get shifted char

0

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:

  • (QWERTY) 2 => @; 3 => #; etc...
  • (AZERTY) & => 1; é => 2; etc...

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.

Manitoba

Posted 2012-06-18T09:10:47.070

Reputation: 5 691

Answers

0

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.

Manitoba

Posted 2012-06-18T09:10:47.070

Reputation: 5 691

1

Try using KeyEvent#getKeyChar(). For an event Shift+a, this returns A. But this only works in the KeyListener#keyTyped() method, not in the other ones (keyReleased/keyPressed). Here is a little example, that works very well. I tried different keyboard layouts. Your example (Shift+2) return @ in qwerty, etc.

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class Maion {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(600, 400);
        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                System.out.println("keyTyped: '" + e.getKeyChar() + "'");
            }
        });
        frame.setVisible(true);
    }
}

brimborium

Posted 2012-06-18T09:10:47.070

Reputation: 7 040

Nope, this just gives me the key code, not the shifted one. But the modifier is well defined. – Manitoba – 2012-06-18T09:41:09.807

@Vodemki I added a little example. It works fine... I get @ for Shift+2 with qwerty... – brimborium – 2012-06-18T09:57:24.507

Well, my problem is i'm catching the key code using a JNI code. So I have a function named nativeKeyTyped which receives a NativeKeyEvent. This event works exactly the same way as the KeyEvent you gave me. If I read the modifiers when a key is typed, it gives me "0" when the shift key is NOT pressed and "1" when it is. But when I read the key code with getKeyChar always returns me a 'small' character. – Manitoba – 2012-06-18T10:02:44.507

1@Vodemki I think that has to do with the fact, that KeyEvent#getKeyChar() only "works" for KeyEvent.KEY_TYPED events. Is your event a KeyEvent.KEY_TYPED event? If not, you either have to change your JNI code (if accessible), manually convert your key event into the correct character or use a different method of gathering key events. – brimborium – 2012-06-18T10:10:26.107

Ok, thanks. As I can't edit the JNI code, I'll manually convert each key. This sounds to be the easiest way. – Manitoba – 2012-06-18T10:14:49.680

@Vodemki You can check the key event type with KeyEvent#getID() (Constants are KeyEvent.KEY_RELEASED/KEY_TYPED/KEY_PRESSED). Only the keyTyped event works with KeyEvent#getKeyChar(). – brimborium – 2012-06-18T12:16:42.350

0

Look at the method getModifiers() in the class KeyEvent. This will tell you which special keys are pressed (Alt, Ctrl, Shift)

Example:

boolean oneShift = (keyEvent.getKeyCode() == KeyEvent.VK_1) && "Shift".equals(KeyEvent.getKeyModifiersText(keyEvent.getModifiers()))

raven1981

Posted 2012-06-18T09:10:47.070

Reputation: 910

I've already that code. All i need is the associated key code of a key pressed while shift was pressed. Ex: SHIFT + 1 gives me ! – Manitoba – 2012-06-18T09:24:47.440

Your example will only work if the user is only holding down Shift. The proper way (using getModifiers()) would be boolean shift = (keyEvent.getModifiers() & KeyEvent.VK_SHIFT) == KeyEvent.VK_SHIFT;. – a CVn – 2012-06-18T09:26:23.097

Besides the fact that this is not what the OP asked for. ;) – brimborium – 2012-06-18T09:28:13.977

Yeah, because I made the listener using JNI. So yes, the user will have to hold down shift in order to use the command. But this is not the problem here. Is there another way to get the shifted character than using a switch/case? I mean a kind of function in java which would take the key code and returns a shifted one. – Manitoba – 2012-06-18T09:32:26.097

I'm sorry, I'm wrong – raven1981 – 2012-06-18T09:33:18.980