-1

I noticed that I get two different keyCode for the same Char. Here is a little experiment:

package main;

import com.sun.javafx.scene.control.Keystroke;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Keystroketest {
    public static void main(String[] args) {
        final KeyStroke ks = KeyStroke.getKeyStroke('e', 0);
        System.out.println(ks.getKeyCode());

        JFrame f = new JFrame();
        JTextField jtf = new JTextField("");
        f.setBounds(300, 300, 100, 60);
        f.add(jtf);
        f.setVisible(true);


        jtf.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent keyEvent) {
            }

            @Override
            public void keyPressed(KeyEvent keyEvent) {
            }

            @Override
            public void keyReleased(KeyEvent keyEvent) {
                System.out.println(keyEvent.getKeyCode());
                if (keyEvent.getKeyCode() == ks.getKeyCode()) {
                    System.out.println("letters are similar");
                } else {
                    System.out.println("letters aren't similar");
                }
            }
        });
    }
}

If I type in the textField the letter "e" so it returns a different KeyCode for the same letter when I parse it.

Whats the reason? And how can I check if the letter/char I typed is the same as in a specific char defined in the code as above...?

So whenever I check the KeyCode I typed, java thinks I didn't type the same letter. But that's not correct I think.

Synoon
  • 2,297
  • 4
  • 22
  • 37
  • 1
    You really shouldn't be using `KeyListener` on text fields, and if you're interested in filtering the content, you should use a `DocumentFilter` – MadProgrammer Jul 24 '14 at 11:25
  • is Stateless what does that mean? – Synoon Jul 24 '14 at 11:27
  • 1
    It means that the virtual key doesn't have any information to differentiate it from `E` or `e` – MadProgrammer Jul 24 '14 at 11:29
  • But the actual problem is with the how you are using `getKeyStroke`. The method you are using actually calls for an `int` not a `char`, which represents the virtual key code. An attempt to use `KeyStroke.getKeyStroke(char)` will result in a virtual key code of `0`. Generally, it's very hard to perform this kind of reverse look up – MadProgrammer Jul 24 '14 at 11:34
  • yes I noticed that already, now I changed my structure and check not the KeyCode but the KeyChar with Char i haven't got any problem. – Synoon Jul 24 '14 at 12:11

1 Answers1

0

You are using the wrong getKeyStroke method:

final KeyStroke ks = KeyStroke.getKeyStroke('e', 0);

Calls getKeyStroke(int keyCode, int modifiers), since a "char" is interpreted as a 16 bit value that can be autocast to a 32 bit int value. Autoboxing does only happen if there isn't a fitting method.

use either

final KeyStroke ks = KeyStroke.getKeyStroke(Character.valueOf('e'), 0);

or:

final KeyStroke ks = KeyStroke.getKeyStroke('e');

And remember for your own APIs: try to detect such clashes when overloading. ;-)

Also, like MadProgrammer pointed out that won't resolve your problem.

Have you tried using keyEvent.getKeyChar() instead?

Hardcoded
  • 6,476
  • 2
  • 22
  • 20
  • While your observations are correct `KeyStroke.getKeyStroke('e')` or `KeyStroke.getKeyStroke(Character.valueOf('e'), 0)` result in a `keycode` of `0`, not `VK_E` or `69`... – MadProgrammer Jul 24 '14 at 11:31