我終於解決了我的問題。
我使用下面的代碼片段來獲取NetBeans編輯器的活動JTextComponent,然後將KeyEventListeners添加到它,並且它完美地工作。
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;
}
}
});
所以這個插件,現在允許我捕捉與利用的關鍵事件活動的NetBeans代碼編輯器的用戶交互。