I ran into the same problem today: I wanted to catch Ctrl + =, which we press thinking of Ctrl + +, and associate it to a zoom in action. I use a Brazilian ABNT2 keyboard. While typing, to obtain the plus character, I need to use the combination Shift + =, so I can't catch Ctrl + + directly. I could do like @Aqua suggested, which is to actually catch Ctrl + Shift + =, but it does not seem natural to me. I decided to see how some applications solve that problem.
Notepad++ associates zoom in and zoom out to the numpad's plus and minus, respectively. That's an easy solution to the problem, but it was also not what I wanted. Mozilla Firefox, by its turn, does exactly what I want: it says that Ctrl + + is the key combination for zooming in, but what it actually catches is Ctrl + =. Additionally, it also understands if I use the numpad's plus to zoom in.
How I solved the problem
So, that's how I decided to solve the problem: while creating the Action
, I associated the key combination Ctrl + + to the action of zooming in, which actually can't be caught:
Action zoomInAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
zoomIn();
}
};
zoomInAction.putValue(AbstractAction.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, KeyEvent.CTRL_DOWN_MASK));
JMenuItem zoomInMenuItem = new JMenuItem(zoomInAction);
viewMenu.add(zoomInMenuItem);
The ace in the hole is to catch the Ctrl + = combination apart and treat it the same:
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent event) {
}
@Override
public void keyReleased(KeyEvent event) {
}
@Override
public void keyPressed(KeyEvent event) {
if (event.isControlDown() && (event.getKeyCode() == KeyEvent.VK_EQUALS)) {
zoomIn();
}
}
});
That way, the interface (i.e. the JMenuItem
that corresponds to the Action
) tells the user to use the key shortcut Ctrl + + to zoom in. The user then presses Ctrl + =, thinking of Ctrl + +, but the application understands that combination and acts as the user expects it to do so.
This is my first Stack Overflow answer, so sorry for anything :)