Java detect CTRL+X key combination on a jtree
Asked Answered
C

5

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.

Cuirass answered 11/5, 2011 at 21:20 Comment(2)
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.Leela
See camickr's answer for the working link to the Key Bindings tutorial.Molli
R
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) {
            }
        });
Robins answered 11/5, 2011 at 21:53 Comment(6)
Use Toolkit.getMenuShortcutKeyMask() instead of hard-coding CTRL_MASK. Not all platforms use Ctrl as the modifier for commands.Issuable
Key Bindings are the approach used by all Swing components.Leela
InputEvent.CTRL_MASK is deprecated-ish, the extended modifiers (e.g. InputEvent.CTRL_DOWN_MASK) are recommended for use now. Annoying but true. docs.oracle.com/javase/6/docs/api/java/awt/event/…Mccormac
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!"); } }Spellbinder
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.Molli
how about if i press CTRL + ALT + A ?Petrarch
T
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.

Tetracycline answered 11/5, 2011 at 22:4 Comment(1)
Key Bindings are the approach used by all Swing cvomponents.Leela
L
3

Use Key Bindings.

Leela answered 11/5, 2011 at 23:46 Comment(0)
S
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;
        }
    });
Syllable answered 13/6, 2015 at 17:27 Comment(1)
You should also provide an explanation of how the code works.Mealworm
A
0

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

Australian answered 11/5, 2011 at 21:42 Comment(2)
System reserved Key Combinations seems conflict Accelerator. i did it but its not work. any chance to add a keyboard handler to detect?Cuirass
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.Australian

© 2022 - 2024 — McMap. All rights reserved.