-
Bug
-
Resolution: Won't Fix
-
P3
-
7u6, 7u7, 8
-
os_x
FULL PRODUCT VERSION :
java version " 1.7.0_07 "
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Mac OS X 10.7.4
Darwin Kernel Version 11.4.0: RELEASE_X86_64 x86_64
A DESCRIPTION OF THE PROBLEM :
The Alt-key modifier does not work properly in keybindings on Mac OSX with Java 7. When a textfield is present (and focussed), the textfield consumes the KeyEvent that is modified with the Alt-key and the keybinding is thus not executed.
The Alt-key is used on the Mac to enter special characters (e.g. Alt+C results in a c-cedilla). This special character is then also displayed in the textfield, when focussed. The keybinding, however, is not executed.
Compare this to using a keybinding with the Shift-key as modifier. Here the textfield shows the capital letter, but also executes the keybinding.
Also note that in JRE6, a keybinding with the Alt-key as modifier is executed properly.
REGRESSION. Last worked in version 7
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a GUI with a JTextField and a JButton
2. Attach a keybinding to the JButton that uses the Alt-key as modifier (e.g. Alt+C). Note that the keybinding should be on the WHEN_IN_FOCUSED_WINDOW input map.
3. Implement an Action for the keybinding that can be used to check that the keybinding is executed (e.g. print to console)
3. Run application and try the keybinding when: a) focussed on the textfield and b) focussed on the button
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The Action associated with the keybinding should be executed in all cases
ACTUAL -
The Action is only executed when not focussed on the textfield
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class KeyBindingBug extends JPanel
{
public KeyBindingBug()
{
JLabel label = new JLabel( " Bindings: Alt+C and Shift+O " );
JTextField textField = new JTextField( " Text here " , 20);
add(label);
add(textField);
Action doSomething = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
System.out.println( " actionPerformed. Modifiers are: " + e.getModifiers() + " ; " );
}
};
JButton button = new JButton( " Button " );
KeyStroke stroke1 = KeyStroke.getKeyStroke('O', InputEvent.SHIFT_MASK | InputEvent.SHIFT_DOWN_MASK);
KeyStroke stroke2 = KeyStroke.getKeyStroke('C', InputEvent.ALT_MASK | InputEvent.ALT_DOWN_MASK);
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke1, " doSomething " );
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke2, " doSomething " );
button.getActionMap().put( " doSomething " , doSomething);
add(button);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame( " KeyBinding OSX Bug " );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new KeyBindingBug());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Did not found a workaround
java version " 1.7.0_07 "
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Mac OS X 10.7.4
Darwin Kernel Version 11.4.0: RELEASE_X86_64 x86_64
A DESCRIPTION OF THE PROBLEM :
The Alt-key modifier does not work properly in keybindings on Mac OSX with Java 7. When a textfield is present (and focussed), the textfield consumes the KeyEvent that is modified with the Alt-key and the keybinding is thus not executed.
The Alt-key is used on the Mac to enter special characters (e.g. Alt+C results in a c-cedilla). This special character is then also displayed in the textfield, when focussed. The keybinding, however, is not executed.
Compare this to using a keybinding with the Shift-key as modifier. Here the textfield shows the capital letter, but also executes the keybinding.
Also note that in JRE6, a keybinding with the Alt-key as modifier is executed properly.
REGRESSION. Last worked in version 7
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a GUI with a JTextField and a JButton
2. Attach a keybinding to the JButton that uses the Alt-key as modifier (e.g. Alt+C). Note that the keybinding should be on the WHEN_IN_FOCUSED_WINDOW input map.
3. Implement an Action for the keybinding that can be used to check that the keybinding is executed (e.g. print to console)
3. Run application and try the keybinding when: a) focussed on the textfield and b) focussed on the button
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The Action associated with the keybinding should be executed in all cases
ACTUAL -
The Action is only executed when not focussed on the textfield
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class KeyBindingBug extends JPanel
{
public KeyBindingBug()
{
JLabel label = new JLabel( " Bindings: Alt+C and Shift+O " );
JTextField textField = new JTextField( " Text here " , 20);
add(label);
add(textField);
Action doSomething = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
System.out.println( " actionPerformed. Modifiers are: " + e.getModifiers() + " ; " );
}
};
JButton button = new JButton( " Button " );
KeyStroke stroke1 = KeyStroke.getKeyStroke('O', InputEvent.SHIFT_MASK | InputEvent.SHIFT_DOWN_MASK);
KeyStroke stroke2 = KeyStroke.getKeyStroke('C', InputEvent.ALT_MASK | InputEvent.ALT_DOWN_MASK);
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke1, " doSomething " );
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke2, " doSomething " );
button.getActionMap().put( " doSomething " , doSomething);
add(button);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame( " KeyBinding OSX Bug " );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new KeyBindingBug());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Did not found a workaround