1

I want to associate ctrl + shift + numpad 7 to an action map. Basically I'm trying to bind my actions to keyboard shortcuts and want the same behavior as of the number keys pressed from top or number keypressed from right numbers of keyboard.

I'm able to map

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");

but cannot map

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");

Here is the code that I am trying to execute:

package testmaik;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class KeyBindingsTest {

public static void main(String[] args) {
    new KeyBindingsTest();
}

public KeyBindingsTest() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

public class TestPane extends JPanel {

    private JLabel state;

    /**
     * 
     */
    public TestPane() {
        setLayout(new GridBagLayout());
        state = new JLabel("Nothing here");
        add(state);

        InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
        im.put(KeyStroke.getKeyStroke("ctrl 7"), "ctrl+7");
        im.put(KeyStroke.getKeyStroke("ctrl shift 7"),  "Ctrl+3");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");

        ActionMap am = getActionMap();
        am.put("Ctrl+1", new MessageAction("Ctrl+1"));
        am.put("Ctrl+2", new MessageAction("Ctrl+2"));
        am.put("Ctrl+3", new MessageAction("Ctrl+3"));
        am.put("ctrl+7", new MessageAction("ctrl+7"));
        am.put("Ctrl+4", new MessageAction("Ctrl+4"));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    public class MessageAction extends AbstractAction {

        private String message;

        public MessageAction(String message) {
            this.message = message;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            state.setText(message);
        }

    }

}

}

Please help.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
Bhavin
  • 31
  • 6
  • I don't seem to have any trouble, in fact, if I register `KeyEvent.VK_1`, the number pad 1 also responds. What OS and Java version are you using? – MadProgrammer Apr 29 '16 at 09:52
  • I am using Windows 7 and Java 7. VK_1 and VK_NUMPAD1 both have different event codes. It cannot register automatically without any code development. Please share if it does. – Bhavin Apr 29 '16 at 10:14
  • I also found that shift and numpad does not work together. When shift is used with numpad keys, it reads as if arrow keys are pressed. If we disable the shift key behaving like arrow movement keys, we can associate the shift with other keys, as I checked associating ctrl alt and numpad7 it was working, – Bhavin Apr 29 '16 at 10:18

1 Answers1

1

An alternate solution: Try this im your TestPane class and you can catch the event itself:

getRootPane().getContentPane().getToolkit().addAWTEventListener(new AWTEventListener() {

        public void eventDispatched(final AWTEvent event) {
            if (event.getID() == KeyEvent.KEY_PRESSED) {
                final KeyEvent keyEvent = (KeyEvent) event;
                switch (keyEvent.getKeyCode()) {
                    case KeyEvent.VK_NUMPAD7:
                        if(keyEvent.isControlDown() && keyEvent.isShiftDown()) {
                            //do Stuff
                        }
                        break;
                }
            }
        }

    }, AWTEvent.KEY_EVENT_MASK);

I am not sure what's wrong with your event map.

huellif
  • 414
  • 4
  • 12
  • I was trying to find the solution using the INPUT MAP and ACTION MAP. We may probably use this, but if we have only input maps and action maps, how should we implement. – Bhavin May 03 '16 at 11:00
  • Sorry I never worked with those maps before and the code looks fine and seems to fit the docs :-\ – huellif May 03 '16 at 12:16