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);
}