Java Keybinding Plus Key

后端 未结 3 797
情歌与酒
情歌与酒 2020-12-16 22:37

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

相关标签:
3条回答
  • 2020-12-16 22:56

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

    0 讨论(0)
  • 2020-12-16 23:02

    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.

    0 讨论(0)
  • 2020-12-16 23:06

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

    In order to use the + that appears along the top of the keybaord (next to the line of numbers), you would need to phsycially type shift+=

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

    But you also want the KeyEvent.CTRL_DOWN_MASK modifier as well.

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

    The problem you're having is that the modifiers are a bitwise-ored combination of any modifiers

    0 讨论(0)
提交回复
热议问题