Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Keybinding Plus Key

I was trying to create shortcuts for zooming in and out in an image editing application I'm creating and I noticed something strange. To bind the combination of ctrl + +, I had to use the = key and a control and shift mask:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, KeyEvent.CTRL_DOWN_MASK + KeyEvent.SHIFT_DOWN_MASK),"ZoomIn");

Neither of the combinations where I tried to directly bind to VK_PLUS worked:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, KeyEvent.CTRL_DOWN_MASK + KeyEvent.SHIFT_DOWN_MASK),"ZoomIn");

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, KeyEvent.CTRL_DOWN_MASK),"ZoomIn");

It works right now with the very first line of code, but I was wondering why neither of the bottom two work and if this could (theoretically) be a problem if a keyboard did not have the + key as the shifted = key.

like image 276
scaevity Avatar asked Mar 24 '13 22:03

scaevity


2 Answers

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 :)

like image 62
Antônio Medeiros Avatar answered Sep 25 '22 17:09

Antônio Medeiros


For numeric keypad plus try KeyEvent.VK_ADD:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,
                KeyEvent.CTRL_DOWN_MASK), "plus");

For plus on main keyboard (US keyboard layout) use:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK),"plus"); 

For non US keyboard use VK_PLUS. See bugs 4262044 and 6942481 for some clarifications.

like image 28
tenorsax Avatar answered Sep 22 '22 17:09

tenorsax