How can a KeyListener detect key combinations (e.g., ALT + 1 + 1)

前端 未结 6 1373
悲哀的现实
悲哀的现实 2020-11-27 07:02

How can I let my custom KeyListener listen for combinations of ALT (or CTRL for that matter) + more than one other key?

Assume

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 07:29

    import java.awt.*;
    import java.awt.event.*;
    class KDemo
    {
         public static void main(String args[])
         {
               Frame f = new Frame();
               f.setSize(500,500);
               f.setVisible(true);
               f.addKeyListener(new KeyAdapter()
               {
                   public void keyPressed(KeyEvent e)
                   {
                       AWTKeyStroke ak = AWTKeyStroke.getAWTKeyStrokeForEvent(e);
                       if(ak.equals(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK)))
                       {
                         System.exit(0);
                       }
                   }
               });
            }
       }
    

提交回复
热议问题