21

I need an example how to add a keyboard handler that detect when Ctrl+C , Ctrl+X , Ctrl+C pressed on a JTree.

I were do this before with menu shortcut keys but with no success.

Sastrija
  • 3,284
  • 6
  • 47
  • 64
ShirazITCo
  • 1,041
  • 6
  • 23
  • 38
  • Although these suggestions will work, they really deserve a -1. Swing was designed to be used with Key Bindings, not KeyListeners. See: download.oracle.com/javase/tutorial/uiswing/misc/…. Solutions should promote standard Swing design concepts. Using KeyListeners is used on older AWT applications which don't support key bindings. – camickr May 11 '11 at 23:55
  • See [camickr's](http://stackoverflow.com/a/5971832/1048186) answer for the working link to the Key Bindings tutorial. – Josiah Yoder Feb 02 '16 at 19:37

5 Answers5

34

You can add KeyListeners to any component (f)

        f.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if ((e.getKeyCode() == KeyEvent.VK_C) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
                    System.out.println("woot!");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • 3
    Use Toolkit.getMenuShortcutKeyMask() instead of hard-coding CTRL_MASK. Not all platforms use Ctrl as the modifier for commands. – John M May 11 '11 at 21:55
  • Key Bindings are the approach used by all Swing components. – camickr May 11 '11 at 23:42
  • 1
    `InputEvent.CTRL_MASK` is deprecated-ish, the extended modifiers (e.g. `InputEvent.CTRL_DOWN_MASK`) are recommended for use now. Annoying but true. http://docs.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html#CTRL_MASK – ericsoco Feb 11 '13 at 17:46
  • `if ((e.getKeyCode() == KeyEvent.VK_C) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) { System.out.println("woot!"); } }` Would treat "Alt+CTRL+C" = "CTRL+C". The proposed sulution actually can't distinguish from any multiple modifiers. Use rather `@Override public void keyPressed(KeyEvent e) { if ((e.getKeyCode() == KeyEvent.VK_C) && ((e.getModifiers() | KeyEvent.CTRL_MASK) == KeyEvent.CTRL_MASK)) { System.out.println("woot!"); } }` – Hans Apr 10 '15 at 10:41
  • For some reason, in my testing Ctl-A shows up with KeyCode 0 and KeyChar 1. Similarly, Ctl-N shows up with KeyChar 14. (The number of the letter in the alphabet.) Don't know if this is documented somewhere. – Josiah Yoder Feb 02 '16 at 19:34
  • how about if i press CTRL + ALT + A ? – gumuruh Mar 15 '17 at 14:22
13

Use KeyListener for example :

jTree1.addKeyListener(new java.awt.event.KeyAdapter() {

        public void keyPressed(java.awt.event.KeyEvent evt) {
            if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_C) {

                JOptionPane.showMessageDialog(this, "ctrl + c");

            } else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_X) {

                JOptionPane.showMessageDialog(this, "ctrl + x");

            } else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_V) {

                JOptionPane.showMessageDialog(this, "ctrl + v");

            }
        }
    });

Hope that helps.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
MOHAMED FATHEI
  • 470
  • 2
  • 5
3
    initComponents();
      KeyboardFocusManager ky=KeyboardFocusManager.getCurrentKeyboardFocusManager();

    ky.addKeyEventDispatcher(new KeyEventDispatcher() {

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {

             if (e.getID()==KeyEvent.KEY_RELEASED && (e.getKeyCode() == KeyEvent.VK_C) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
                System.out.println("Dhanushka Tharindu");
            }
             return  true;
        }
    });
3

Use Key Bindings.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

But menu shortcut accelerators are the way to do this normally: myMenuItem.setAccelerator(KeyStroke.getKeyStroke("control C"));

Joel
  • 3,427
  • 5
  • 38
  • 60
  • System reserved Key Combinations seems conflict Accelerator. i did it but its not work. any chance to add a keyboard handler to detect? – ShirazITCo May 11 '11 at 21:51
  • yes you can add a keylistener to the control, implement the onKeyPress() and check for the modifiers of the keyevent. I did it to detect a mousewhell + ctrl, but it should work as well in your case. – Joel May 11 '11 at 21:54