JDK-8087700 : [KeyCombination, Mac] KeyCharacterCombinations behave erratically
  • Type: Bug
  • Component: javafx
  • Sub-Component: window-toolkit
  • Affected Version: 8u40
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-09-26
  • Updated: 2023-11-27
  • Resolved: 2023-11-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
jfx22 b18Fixed
Related Reports
Relates :  
Relates :  
Description
Steps:

1) Run TestKeyCombination
2) Press Control+-
3) BUG: On Mac, only one key event is fired ("Hi" is printed twice on Windows_

Here is the test code:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCharacterCombination;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.Stage;

public class TestKeyCombination extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override public void start(Stage stage) {
        stage.setTitle("Test KeyCombination");
        Button button1 = new Button();
        button1.setText("Click Me");
        Scene scene = new Scene(new Group(button1), 600, 450);
        stage.setScene(scene);
        stage.show();
        
        KeyCombination cmdMinus = new KeyCodeCombination(KeyCode.MINUS, KeyCombination.CONTROL_DOWN);
        KeyCombination cmdMinusFromCharacter = new KeyCharacterCombination("-", KeyCombination.CONTROL_DOWN);
        Runnable runnable = () -> System.out.println("HI");
        scene.getAccelerators().put(cmdMinus, runnable);
        scene.getAccelerators().put(cmdMinusFromCharacter, runnable);
    }
}

Comments
Changeset: cda623d7 Author: Martin Fox <mfox@openjdk.org> Committer: Kevin Rushforth <kcr@openjdk.org> Date: 2023-11-16 15:53:48 +0000 URL: https://git.openjdk.org/jfx/commit/cda623d74a17c1aac034bac175e4fb10bd1fd52b
16-11-2023

The original bug report is rather terse. Here is a more complete description. A KeyCharacterCombination should match a key if the target character is printed on that key. For example, the user should be able to invoke the Shortcut+'+' combination by holding down the Shortcut key and pressing a key that has + printed on it. This should work even if + is a shifted symbol but the user doesn't hold down the Shift key. For example, on a U.S. English keyboard when the user presses Shortcut+'=' on the main keyboard it should match a KeyCharacterCombination targeting the + sign. JavaFX is expected to figure out that the = key can generate + even though the user typed =. Steps to reproduce using the original test code: - Switch to a U.S. English keyboard layout - Run TestKeyCombination.java - Press Control and the minus key (-) on the main keyboard, not the numeric keypad Expected results: - You should see the phrase "HI" printed twice on the console Actual results: - "HI" only appears once The original test code uses Control which is odd on the Mac. It also uses a KeyCodeCombination which muddies the water since it can behave differently on different layouts. The test file KeyCharCombinations.java uses Cmd which is more appropriate on a Mac and just tests KeyCharacterCombinations. Steps to reproduce: - Switch to a U.S. English keyboard layout - Run KeyCharCombinations.java - Press Command and the equals key (=) on the main keyboard, not the numeric keypad Expected results: - You should see the phrase "Shortcut + Plus" printed on the console Actual results: - Nothing is printed Note: it's important to run these tests by launching a fresh JFX app and immediately testing these combinations. If you start typing characters JFX may figure out the mappings and things will start working correctly (at least until you type the same characters on the numeric keypad at which point the main keyboard will start failing again. It's very confusing.)
15-11-2023

Users who switch between multiple keyboard layouts can encounter another issue. The map that Glass builds internally isn't cleared during the switch so it can contain stale entries. While testing this it helps to go to the keyboard icon in the menu bar and select "Show Keyboard Viewer" so you see can the logical layout. Steps to Reproduce: Run KeyCharCombinations.java. Switch to the standard German layout. Press Cmd+'+'. If your physical keyboard is U.S. English this is the key that has ] printed on it. You should see "Shortcut + Plus" printed which is correct. Switch to the U.S. layout. Press Cmd+']' (the same physical key) Expected Results: No output should be generated Actual Result: You will see "Shortcut + Plus" printed even though on this layout that key can't generate a plus sign.
14-11-2023

There is a manual test for this now. See tests/manual/events/KeyboardTest.java. When testing select "without keypad combinations" to test KeyCharacterCombinations without the numeric keypad and "with all combinations" to include the numeric keypad.
14-11-2023

Bumping priority. The title doesn't describe the extent of the problem. Accelerators involving punctuation like '+' don't work reliably (so even common accelerators like Cmd+'+' are iffy) and though there is a work-around I doubt most end users would figure it out.
16-08-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jfx/pull/1209 Date: 2023-08-14 16:28:20 +0000
14-08-2023

The Mac glass code figures out which characters are on which keys as the user types. When you type Ctrl+'-' the platform code is seeing some ASCII control code, not a minus sign, so it learns nothing useful. For the glass code to learn that '-' is on that key you have to type it without any modifiers at which point the accelerator will start working. But press '-' on the keypad and the association will move over there and the accelerator will no longer work with the main keyboard. This is also true for accelerators involving Cmd. If you launch a JavaFX app and type Cmd+'+' on the main keyboard using a U.S. English layout the platform code will see Cmd+'=' and will not learn that '+' is on the same key.
25-04-2023

Attached test case provided by Scott.
26-09-2014

See also RT-27602
26-09-2014

Need to investigate to determine what the correct behaviour is and make it consistent between platforms (including Linux).
26-09-2014