请问如何才能探测到组合键?
我知道使用下面的方法可以探测哪一个按键被按下,但是这种方法只能探测单个按键被按下,如果是Ctrl+A这样的组合键就无法探测了。请问如何才能探测到组合键?
class KeyHandler extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
Point2D.Double temp_center;
boolean re=true;
double ang=0;
switch (keyCode) {
case KeyEvent.VK_ADD://按下数字键盘上的+
case KeyEvent.VK_PAGE_UP://按下PageUp键
break;
case KeyEvent.VK_SUBTRACT://按下数字键盘上的-
case KeyEvent.VK_PAGE_DOWN://按下PageDown
}
}
}
[解决办法]
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Gui10 extends Panel {
public Gui10(String title) {
Frame frame = new Frame(title);
frame.setSize(200, 400);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
init();
frame.add(this);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void init() {
final TextArea textArea = new TextArea(10, 20);
textArea.setBackground(Color.CYAN);
textArea.setText("test");
this.add(textArea);
textArea.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("arg0.getModifiers()=" + e.getModifiers());
System.out.println("arg0.getKeyCode()=" + e.getKeyCode());
// CTRL+C按下
if (e.getModifiers() == 2 && e.getKeyCode() == 67) {
System.out.println("CTRL+C......");
}
}
@Override
public void keyPressed(KeyEvent e) {
}
});
}
public static void main(String[] args) {
new Gui10("demo");
}
}