KeyListeners and TopComponents

Just like JPanels, TopComponents are not focusable by default. They need to be explicitly defined as such, as shown below. Then key listening works, though remember that the TopComponent needs to be the currently active TopComponent, otherwise some other TopComponent will catch your key events.

...
...
...
public final class DemoTopComponent extends TopComponent {

    public DemoTopComponent() {
        initComponents();
        setName(Bundle.CTL_DemoTopComponent());
        setToolTipText(Bundle.HINT_DemoTopComponent());
        setFocusable(true);
        addKeyListener(new MyKeyAdapter());
    }

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

    ...
    ...
    ...

Helpful reference which caused me to see the light: http://stackoverflow.com/questions/3728035/java-tracking-keystrokes-with-inputmap

Comments:

Hello,

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?

Any help would be appreciated.

Regards,
Jeremie

Posted by Jeremie on August 27, 2014 at 01:42 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« March 2015
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today