Accelerators - odd behavior
Tomas Mikula
tomas.mikula at gmail.com
Fri Sep 26 18:19:34 UTC 2014
Here is a possible solution to Scott's original problem:
KeyCombination CtrlMinus = new KeyCombination(CONTROL_DOWN) {
@Override
public boolean match(KeyEvent event) {
return super.match(event)
&& event.getEventType() == KEY_PRESSED
&& (event.getCode() == MINUS || event.getCode() ==
SUBTRACT);
}
};
menuItem.setAccelerator(CtrlMinus);
On Fri, Sep 26, 2014 at 8:05 PM, Tomas Mikula <tomas.mikula at gmail.com> wrote:
> The cause of the problem seems clear to me. This is what I observe on
> my (Linux) system.
>
> Pressing '-' on the main part of the keyboard produces MINUS key code
> pressed. Pressing '-' on the numeric keypad produces SUBTRACT key code
> pressed.
>
> KeyCharacterCombination("-") matches MINUS pressed, but does not match
> SUBTRACT pressed (this is the part that may vary across systems, I
> think. If on Mac it match SUBTRACT and not MINUS, it explains the
> problem).
>
> So in Scott's test case, when I press ordinary minus, MINUS pressed
> event is fired, and both KeyCodeCombination(MINUS) and
> KeyCharacterCombination("-") match, so the runnable is executed twice.
>
> When, on the other hand, I pres minus on numeric keypad, SUBTRACT
> pressed event is fired, and only KeyCodeCombination(SUBTRACT) matches,
> so the runnable is executed only once.
>
> To test this explanation, can someone please run the following program
> on a Mac and post its output?
>
>
> import static javafx.scene.input.KeyCode.*;
> import static javafx.scene.input.KeyEvent.*;
> import javafx.application.Platform;
> import javafx.embed.swing.JFXPanel;
> import javafx.scene.input.KeyCharacterCombination;
> import javafx.scene.input.KeyEvent;
>
>
> public class KeyCodeForMinus {
>
> public static void main(String[] args) {
> new JFXPanel();
>
> KeyCharacterCombination minusChar = new KeyCharacterCombination("-");
> KeyEvent minusPressed = new KeyEvent(KEY_PRESSED, "", "",
> MINUS, false, false, false, false);
> KeyEvent subtractPressed = new KeyEvent(KEY_PRESSED, "", "",
> SUBTRACT, false, false, false, false);
>
> System.out.println("'-' matches MINUS: " +
> minusChar.match(minusPressed));
> System.out.println("'-' matches SUBTRACT: " +
> minusChar.match(subtractPressed));
>
> Platform.exit();
> }
>
> }
>
> On Fri, Sep 26, 2014 at 7:44 PM, Scott Palmer <swpalmer at gmail.com> wrote:
>> Please attach the test case I mailed earlier to the JIRA issue.
>>
>> On Fri, Sep 26, 2014 at 1:42 PM, Stephen F Northover <
>> steve.x.northover at oracle.com> wrote:
>>
>>> Agree. Suggest that we move the discussion to the JIRA to capture the
>>> background information for whoever will address the bug.
>>>
>>> Steve
>>>
>>>
>>> On 2014-09-26, 1:41 PM, Kevin Rushforth wrote:
>>>
>>>> It may or may not be a bug, but it will be good to investigate.
>>>>
>>>> -- Kevin
>>>>
>>>>
>>>> Stephen F Northover wrote:
>>>>
>>>>> Two on Windows, one on Mac. See https://javafx-jira.kenai.com/
>>>>> browse/RT-38830
>>>>>
>>>>> Steve
>>>>>
>>>>> On 2014-09-26, 1:25 PM, Stephen F Northover wrote:
>>>>>
>>>>>> This is on Mac. Will try Windows.
>>>>>>
>>>>>> Steve
>>>>>>
>>>>>> On 2014-09-26, 1:24 PM, Kevin Rushforth wrote:
>>>>>>
>>>>>>> Is this on a Mac or on Windows? I just ran your test case and I get
>>>>>>> two runnables, which is what I would expect.
>>>>>>>
>>>>>>> -- Kevin
>>>>>>>
>>>>>>>
>>>>>>> Stephen F Northover wrote:
>>>>>>>
>>>>>>>> I am only seeing the runnable fired once in FX 8u40.
>>>>>>>>
>>>>>>>> Steve
>>>>>>>>
>>>>>>>> Steps:
>>>>>>>>
>>>>>>>> 1) Run TestKeyCombination
>>>>>>>> 2) Press Control+-
>>>>>>>>
>>>>>>>> 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");
>>>>>>>> Scene scene = new Scene(new Group(), 600, 450);
>>>>>>>> Button button1 = new Button();
>>>>>>>> button1.setText("Click Me");
>>>>>>>> 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);
>>>>>>>> }
>>>>>>>> }
>>>>>>>>
>>>>>>>>
>>>>>>>> On 2014-09-26, 1:01 PM, Scott Palmer wrote:
>>>>>>>>
>>>>>>>>> KeyCombination cmdMinus = new Key*Code*Combination(KeyCode.MINUS,
>>>>>>>>> KeyCombination.CONTROL_DOWN);
>>>>>>>>> KeyCombination cmdMinusFromCharacter = new
>>>>>>>>> Key*Character*Combination("-",
>>>>>>>>> KeyCombination.CONTROL_DOWN);
>>>>>>>>>
>>>>>>>>> Using the above like this:
>>>>>>>>> scene.getAccelerators().put(cmdMinus, runnable);
>>>>>>>>> scene.getAccelerators().put(cmdMinusFromCharacter, runnable);
>>>>>>>>>
>>>>>>>>> Will result in the runnable being fired twice from the same keypress.
>>>>>>>>>
>>>>>>>>> I propose changing the accelerator processing logic so that only one
>>>>>>>>> runnable gets called as the intention appears to be that a
>>>>>>>>> KeyCombination
>>>>>>>>> can only have one runnable associated with it, but the logic in Map
>>>>>>>>> doesn't
>>>>>>>>> see the above two KeyCombinations as the same key in the Map.
>>>>>>>>>
>>>>>>>>> Note: With the second combination above I really wanted something
>>>>>>>>> that
>>>>>>>>> worked for both MINUS and SUBTRACT simultaneously - since they both
>>>>>>>>> type
>>>>>>>>> the same Character and only one accelerator can be set on a MenuItem.
>>>>>>>>>
>>>>>>>>> Scott
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>
>>>
More information about the openjfx-dev
mailing list