I have a custom component (inherited from JComponent
) and like to underline a link while holding CTRL pressed right like eclipse does. I decide to use InputMap
and ActionMap
for keypress/release feature.
I use this code to find the stroke:
stroke = KeyStroke.getKeyStroke("pressed CONTROL");
But it is wrong somehow. I debugged the processKeyBinding
method of JComponent and find out that if i press CTRL a KeyStroke
having modifier 130.
(this is my InputMap, arg0 is the incomming KeyStroke from the Keyboard)
I think 130 is the result of the addition of CTRL_DOWN_MASK
who has code 128
and CTRL_MASK
has code 2
.
- Why do i have to add theese modifiers?
- Is this cross-platform?
- Is there any official documentation?
Full example camickr requests:
public class Test extends JLabel {
public static void main(String[] args) {
final JFrame jf = new JFrame("Test");
final Test label = new Test();
jf.getContentPane().add(label);
jf.setBounds(200, 200, 500, 500);
jf.pack();
jf.setVisible(true);
label.grabFocus();
}
public Test() {
super("Foobar");
addBoldOnCtrl();
}
public void addBoldOnCtrl() {
final KeyStroke onDown = KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, KeyEvent.CTRL_MASK + KeyEvent.CTRL_DOWN_MASK);
final String onDownName = "react on ctrl";
getInputMap().put(onDown, onDownName);
getActionMap().put(onDownName, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
final Font f = getFont();
setFont(new Font(f.getFontName(), f.getStyle(), f.getSize() + 2));
repaint();
}
});
}
}