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

F1 as Menu shortcut won't work if textfield has focus

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2
    • Resolution: Duplicate
    • Affects Version/s: 8u45, 8u71
    • Fix Version/s: None
    • Component/s: javafx

      Description

      When using a function key as shortcut for a menu (e.g. F1 for "Help" ... very common) the shortcut won't fire, if a text field is focussed.
       
      The following programm demonstrates the problem:

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.scene.Scene;
      import javafx.scene.control.CheckBox;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.TextArea;
      import javafx.scene.input.KeyCombination;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class F1ShortcutBug extends Application {

      public F1ShortcutBug() {
      }

      @Override
      public void start(Stage stage) throws Exception {
      stage.setTitle("Menu shortcut bug");
      Scene scene = new Scene(new VBox(), 400, 350);

      MenuBar menuBar = new MenuBar();
      Menu menu = new Menu("Help");
      menuBar.getMenus().addAll(menu);
      MenuItem menuItem = new MenuItem("showHelp");
      menuItem.setAccelerator(KeyCombination.valueOf("F1"));
      menuItem.setOnAction((e) -> { System.out.println("Showing help ..."); });
      menu.getItems().addAll(menuItem);

      TextArea textarea = new TextArea("quidquid agis,\nprudenter agas\net respice finem");
      CheckBox checkbox = new CheckBox();

      ((VBox) scene.getRoot()).getChildren().addAll(menuBar, textarea, checkbox);

      stage.setScene(scene);
      stage.show();

      Platform.runLater(() -> { textarea.requestFocus(); });
      }

      public static void main(String[] args) {
      launch(args);
      }
      }

      As long as the textarea has the focus, F1 won't do anything. If on the other hand the checkbox has got focus, F1 will work fine.

      The reason for this behaviour lies within TextInputControlBehavior:

              TEXT_INPUT_BINDINGS.add(new KeyBinding(null, KEY_PRESSED, "Consume"));

      This will consume any unmodified key event.

      One possible workaround is to call
      TEXT_INPUT_BINDINGS.remove(TEXT_INPUT_BINDINGS.size() - 1);
      in a private class derived from TextInputControlBehavior which will remove the default KeyBinding, but this is hacky and will probably change other behaviour as well.



        Attachments

          Issue Links

            Activity

              People

              Assignee:
              leifs Leif Samuelsson (Inactive)
              Reporter:
              rlichten Robert Lichtenberger
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: