Java detect CTRL+X key combination on a jtree

后端 未结 5 1128
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 01:02

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

相关标签:
5条回答
  • 2020-12-11 01:21
        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;
            }
        });
    
    0 讨论(0)
  • 2020-12-11 01:31

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

    0 讨论(0)
  • 2020-12-11 01:37

    Use Key Bindings.

    0 讨论(0)
  • 2020-12-11 01:40

    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.

    0 讨论(0)
  • 2020-12-11 01:44

    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) {
                }
            });
    
    0 讨论(0)
提交回复
热议问题