Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8123632

KeyCodes do not reflect the keys that are actually pressed on German keyboard

XMLWordPrintable

      see RT-20144 which was closed as "not an issue"

      this actually *is* an issue, and an inherently broken issue!

      consider a german keyboard layout (it looks like this: http://www.onepoyle.net/german/support/KB_Germany.png, notice that the EQUALS "=" and PLUS "+" are different physical keys!)

      also consider following application that displays KeyCodes as they are delivered in Swing:

      === KeyEventTest.java ====

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.EventQueue;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      import java.util.Locale;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.KeyStroke;

      public class KeyEventTest {
        public static void main( String[] args ) {
          EventQueue.invokeLater(new Runnable() {
            public void run() {
              Locale.setDefault(Locale.GERMANY);
              (new KeyEventTest()).start();
            }
          });
        }

        private void start() {
          final JTextArea jta = new JTextArea();
          jta.addKeyListener(new KeyListener() {
            public void keyTyped( final KeyEvent e ) {
            }

            public void keyPressed( final KeyEvent e ) {
            }

            public void keyReleased( final KeyEvent e ) {
              System.out.println(KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiersEx()));
            }
          });
          final JScrollPane jsp = new JScrollPane(jta);
          jsp.setPreferredSize(new Dimension(640, 480));

          final JPanel contentPane = new JPanel(new BorderLayout());
          contentPane.add(jsp);

          final JFrame frame = new JFrame(KeyEventTest.class.getName());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(contentPane);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
        }
      }

      pressing the numpad + and the + left of return outputs the following:

      pressed ADD
      pressed PLUS

      now consider the following equivalent javafx program:

      === KeyCodeTest.java ===

      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.input.KeyEvent;
      import javafx.scene.layout.StackPane;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;

      import java.util.Locale;


      public class KeyCodeTest extends Application {

        public static void main(String[] args) {
          Locale.setDefault(Locale.GERMANY);
          launch(args);
        }

        @Override
        public void start(Stage primaryStage) {
          StackPane pane = new StackPane();

          final Label label = new Label("");

          pane.getChildren().add(label);

          pane.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent keyEvent) {
              label.setText(label.getText()+keyEvent.getText());
              System.out.println("pressed \""+keyEvent.getCode()+"\"");
            }
          });

          Scene scene = new Scene(pane, 500, 500);
          primaryStage.setScene(scene);
          primaryStage.show();
          pane.requestFocus();
        }
      }

      same input as above results:

      pressed "ADD"
      pressed "EQUALS"

      note that the keycode indicates a key that actually has *nothing to do* with the key that was physically pressed!

      the workaround that was suggested by Pavel Safrata on the issue that i linked above is incorrect, since there is no way to distinguish between the numpad "+" and the other "+" left of return.

            anthony Anthony Petrov (Inactive)
            srheinnecjfx Sebastian Rheinnecker (Inactive)
            Votes:
            2 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: