我想监听键盘字符,按下ctrl+z时进行打印,请问在代码中ctrl+z如何表示,我用InputEvent.CTRL_MASK+KeyEvent.VK_Z表示运行不正确,用'右箭头字符'运行正确,但我不想用字符表示,想用代码表示,应该如何写?
    public class MonitorText {
JFrame f=new JFrame();
JTextArea text=new JTextArea(10,50);    void init(){
      text.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
System.out.println(e.getKeyChar());
//if(e.getKeyChar()==InputEvent.CTRL_MASK+KeyEvent.VK_Z),这句不行,应该如何表示?
if(e.getKeyChar()==''){//这句可以,''内的字符为右箭头,复制到这里未显示出来,调试是正确的,但我不想用字符想用代码表示
System.out.println("ctrlz");
}
}
});
               f.add(text);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }  public static void main(String[] args)
  {
    new MonitorText().init();
  }}

解决方案 »

  1.   

    mask意思为掩码,那你试试:
    InputEvent.CTRL_MASK | KeyEvent.VK_Z 当然我没搞过Swing,这个是猜测
      

  2.   

    看了一下API的常量值,应该就是这样使用了
      

  3.   

    Action printAction = new AbstractAction("Print"){
        @Override public void actionPerformed(ActionEvent e){
            ...; // print sth.
        }
    };
    printAction.put(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control Z"));
    fileMenu.add(printAction);
    toolbar.add(printAction);
      

  4.   


    我加监听器的本意不是打印,而是调用UndoableEdit实例的undo方法进行撤销操作,原代码还有一句:text.addKeyListener(new KeyAdapter(){
    public void keyTyped(KeyEvent e){
    if(e.getKeyChar()==''){
    undoLis.removeLast().undo();//此句被我简化了
    System.out.println("ctrlz");
    }
    }
    });
    所以你提供的方法不太适合呀。
      

  5.   


    System.out.println((char)(InputEvent.CTRL_MASK|KeyEvent.VK_Z))的结果为Z。按你的方法程序没有反应,即键盘按下ctrl+z没有被识别。
      

  6.   

    判断ctrl按下应该是使用getModifiersEx方法
      

  7.   


    用if((e.getModifiersEx()==InputEvent.CTRL_DOWN_MASK)&&(e.getKeyChar()=='')){
    ...
    }
    或者用
    if((e.getModifiersEx()==InputEvent.CTRL_DOWN_MASK)&&(e.getKeyChar()==KeyEvent.VK_Z)){
    ...
    }
    都不行
      

  8.   

    晕,你看getModifiersEx的文档,不是上面这种判断方法。
      

  9.   

    居然没有注意到 isControlDown这个方法:
    addKeyListener(new KeyAdapter(){
                                @Override public void keyPressed(KeyEvent e){
                                    if(e.isControlDown() && (e.getKeyCode() == 'Z'))
                                        System.out.println("Ctrl + Z pressed");
                                }
                            });
      

  10.   

    看看是否满足你的要求吧。方法帮你改了一下public class MonitorText { static JFrame f = new JFrame();
    static JTextArea text = new JTextArea(10, 50); void init() {
    text.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e){
    if(e.isControlDown() && e.getKeyCode() == e.VK_Z){
    System.out.println("ctrl + z");
    }
    }
    });
    f.add(text);
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } public static void main(String[] args) {
    new MonitorText().init();
    }
    }
      

  11.   

            text.addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e){
                      Boolean ctrl = false;
                       Boolean z= false;
    if (ctrl&&z ) {
    //to do sth
    }
                    if(e.getKeyCode() == KeyEvent.VK_Z){
                        z= true;
                    }
                    if(e.getKeyCode() == KeyEvent.VK_CONTROL){
                       ctrl = true;
                    }
                }
            });试试这个,我没运行过,应该可以满足你的要求
      

  12.   


    成功!keyTyped需要换为keyPressed,但如果将if(e.isControlDown() && e.getKeyCode() == e.VK_Z)换为if(e.isControlDown()&&e.getKeyChar()='z')就不可以,为什么?e.getKeyCode与e.getKeyChar区别是在哪里?有空帮我看看这个
    http://bbs.csdn.net/topics/390331478