I need an example how to add a keyboard handler that detect when Ctrl+C , Ctrl+X , Ctrl+C pressed on a JTree
.
我需要一个如何添加一个键盘处理程序的例子,当Ctrl+C, Ctrl+X, Ctrl+C按在JTree上时,它可以检测。
I were do this before with menu shortcut keys but with no success.
我以前用菜单快捷键做过这个,但是没有成功。
5 个解决方案
#1
23
You can add KeyListeners to any component (f)
可以向任何组件(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) {
}
});
#2
12
Use KeyListener for example :
例如,使用KeyListener:
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.
希望有帮助。
#4
2
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;
}
});
#5
0
But menu shortcut accelerators are the way to do this normally: myMenuItem.setAccelerator(KeyStroke.getKeyStroke("control C"));
但是菜单快捷键加速器是实现这一点的常用方法:myMenuItem.setAccelerator(击键)。getKeyStroke(控制C));
#1
23
You can add KeyListeners to any component (f)
可以向任何组件(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) {
}
});
#2
12
Use KeyListener for example :
例如,使用KeyListener:
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.
希望有帮助。
#3
#4
2
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;
}
});
#5
0
But menu shortcut accelerators are the way to do this normally: myMenuItem.setAccelerator(KeyStroke.getKeyStroke("control C"));
但是菜单快捷键加速器是实现这一点的常用方法:myMenuItem.setAccelerator(击键)。getKeyStroke(控制C));