JDK-8148857 : F1 as Menu shortcut won't work if textfield has focus
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8u45,8u71
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2016-02-02
  • Updated: 2016-09-20
  • Resolved: 2016-02-05
Related Reports
Duplicate :  
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.




Comments
Duplicate of JDK-8089884, which is fixed in 9.
05-02-2016