Java Keybinding Plus Key

I tried to create shortcuts for zooming in and out in the image editing application I am creating and noticed something strange. To bind the ctrl + + combination, I had to use the = key and the control and shift mask:

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

None of the combinations where I tried to bind directly 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 none of the bottom two works, and if this could (theoretically) be a problem if the keyboard did not have the + key as the shifted = .

+6
source share
3 answers

For the numeric keypad plus try KeyEvent.VK_ADD :

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

For a plus on the 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 Errors 4262044 and 6942481 for some explanations.

+11
source

As I understand it, VK_ADD actually used for numpad + .

To use + , which appears at the top of keybaord (next to the string of numbers), you will need to phsycially type shift + =

In this case, you need to use KeyEvent.VK_EQUALS with the KeyEvent.SHIFT_DOWN_MASK modifier.

But you also need the KeyEvent.CTRL_DOWN_MASK modifier.

 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Test1"); 

The problem is that a bitwise-ored combination of any modifiers

+4
source

Today I faced the same problem: I wanted to catch Ctrl + = , which we click on thinking Ctrl + + and associate it with the magnification action. I am using the Brazilian keyboard ABNT2. When typing, to get a plus sign, I need to use the combination Shift + = , so I can not catch Ctrl + + directly. I could do as @Aqua suggested, which should catch Ctrl + Shift + = , but for me this does not seem natural. I decided to see how some applications solve this problem.

Notepad ++ associates scaling and zooming with the numbers plus and minus, respectively. This is an easy solution to the problem, but it was also not what I wanted. Mozilla Firefox , in turn, does exactly what I want: it says that Ctrl + + is a keyboard shortcut for scaling, but what it actually catches is Ctrl + = . In addition, he also understands that I am using numpad plus to increase.

How I solved the problem

So, as I decided to solve the problem: when creating the Action I associated the keyboard shortcut Ctrl + + with the zoom action, which is actually impossible to catch:

 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 combination Ctrl + = and process it the same way:

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

Thus, the interface (i.e. JMenuItem that corresponds to Action ) tells the user to use the Ctrl + + key shortcut to enlarge. Then the user presses Ctrl + = , thinking about Ctrl + + , but the application understands this combination and acts as the user expects it.

This is my first answer, so I apologize for everything :)

+3
source

All Articles