0

In my ULC frame, I implemented some F hotkeys (from F1 to F12). But there can be a little bug, for example if you want to press quickly the F10, maybe you press it with F11 also. I would like to avoid it somehow. Because now it will run both actions for these two keys.

What could be the best solution, if someone press two registered keys, but run only one from them. If this one is the first or the last, it does not matter.

I have tried with synchronized keyword, but it does not help, both command will be executed.

First code example (inner synchronized (parent)):

Button buttonFirst = createButton("F10");
buttonFirst.addActionListener(new IActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        synchronized (parent) {
            System.out.println("F10 - pressed");
            doSomething(1);
        }
    }
});
buttonFirst.addActionKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0, true));

Button buttonSecond = createButton("F11");
buttonSecond.addActionListener(new IActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        synchronized (parent) {
            System.out.println("F11 - pressed");
            doSomething(2);
        }
    }
});
buttonSecond.addActionKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0, true));

Second code example (outter synchronized method):

Button buttonFirst = createButton("F10");
buttonFirst.addActionListener(new IActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        doSomething("F10",1);
    }
});
buttonFirst.addActionKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0, true));

Button buttonSecond = createButton("F11");
buttonSecond.addActionListener(new IActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        doSomething("F11",2);
    }
});
buttonSecond.addActionKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0, true));

private synchronized void doSomething(String keyName, int value) {
    System.out.println(keyName+" - pressed");
    doSomething(value);
}
victorio
  • 6,224
  • 24
  • 77
  • 113
  • it is an ULCButton actually. this is an old framework.. I want this: if both button are pressed at the same time, then after the release, only one action should be done from the 2 buttons. – victorio Apr 13 '17 at 14:18
  • It's out of my area of expertise. Good luck though – Hovercraft Full Of Eels Apr 13 '17 at 14:26
  • @victorio you would have to determine which button should be executed in some way – XtremeBaumer Apr 13 '17 at 14:44
  • 1
    I really doubt if `actionPerformed` for the two buttons would be executed in separate threads for your use case. Having assumed that, I am going to propose a solution. In your `actionPerformed` add the action key to a list, but only if the list `size < 2` ( This is again assuming user doesn't have the fattest finger to press `3` keys at a time ). If `size == 2`, call a method which looks at the list and perform the action for the first key ( or second key if you want ) – SomeDude Apr 13 '17 at 15:11

0 Answers0