1
votes

I'm trying to build a NetBeans module that will consist of an options panel where users must enter their credentials and preferences and of some code that tracks the source code in the main NetBeans editor pane.

The goal is for the plugin to be activated by a successful login of the user and then the plugin will simply listen to pre-defined events triggering its capture of the code in the editor. I don't want the user to have to click an item in the menu bar or some button to trigger the capture of the code. It must be unobtrusive.

I have several options. I would like to have the plugin collect the source code on the Ctrl + S action or the user pressing the Enter key, the Up, Down, Right, Left keys, and/or others.

However, how can I add those kinds of event listeners to the TopComponent or the active NetBeans editor so the user activity in NetBeans is tracked? How can I detect when the users press certain keys in the editor?

1

1 Answers

2
votes

I finally solved my issue.

I used the following snippet of code to get the active JTextComponent of the NetBeans editor and then added KeyEventListeners to it, and it worked perfectly.

JTextComponent jtc = EditorRegistry.lastFocusedComponent();

jtc.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
            int keycode = e.getKeyCode();
            switch (keycode) {
                case KeyEvent.VK_LEFT:
                    JOptionPane.showMessageDialog(null, "1 Left: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_RIGHT:
                    JOptionPane.showMessageDialog(null, "1 Right: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_DOWN:
                    JOptionPane.showMessageDialog(null, "1 Down: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_UP:
                    JOptionPane.showMessageDialog(null, "1 Up: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_ENTER:
                    JOptionPane.showMessageDialog(null, "Enter: "+e.getKeyCode());
                    break;
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {
            int keycode = e.getKeyCode();
            switch (keycode) {
                case KeyEvent.VK_LEFT:
                    JOptionPane.showMessageDialog(null, "2 Left: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_RIGHT:
                    JOptionPane.showMessageDialog(null, "2 Right: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_DOWN:
                    JOptionPane.showMessageDialog(null, "2 Down: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_UP:
                    JOptionPane.showMessageDialog(null, "2 Up: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_ENTER:
                    JOptionPane.showMessageDialog(null, "Enter: "+e.getKeyCode());
                    break;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keycode = e.getKeyCode();
            switch (keycode) {
                case KeyEvent.VK_LEFT:
                    JOptionPane.showMessageDialog(null, "3 Left: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_RIGHT:
                    JOptionPane.showMessageDialog(null, "3 Right: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_DOWN:
                    JOptionPane.showMessageDialog(null, "3 Down: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_UP:
                    JOptionPane.showMessageDialog(null, "3 Up: " + e.getKeyCode());
                    break;
                case KeyEvent.VK_ENTER:
                    JOptionPane.showMessageDialog(null, "Enter: "+e.getKeyCode());
                    break;
            }
        }
    });

So this plugin now allows me to capture the user interaction with the active NetBeans code editor using key events.