Java KeyAdapter

时间:2009-10-27 20:54:13

标签: java keyevent

我对Java KeyAdapter的工作方式有点不熟悉,并且使用KeyAdapter使用以下代码获得意外结果。无论是否调用isKeyPressed(),当按下某个键而另一个键已被按下时,会出现问题。

注意:我知道这是很多代码,我道歉。我尽我所能来隔离它,我认为它主要围绕下面keyHandler方法中的注释(keyHandler()如何将当前按下的键放入keysHeld)。希望彻底的评论很有帮助。

keyHandler:

ArrayList keysHeld = new ArrayList<KeyEvent>();

private void keyHandler()
{
    KeyAdapter keyListnr = new KeyAdapter()
    {
        public void keyPressed(KeyEvent e)
        { 
            int keyCode = e.getKeyCode();

            int index = 0;
            boolean found = false;
            while(!found && index<keysHeld.size()) //While not already found, and end of ArrayList not reached
            {
                System.out.print("errorCheck: keysHeld: "+keysHeld+", "+(Object)keyCode+" "); //PRINT
                if(keysHeld.get(index) == (Object)keyCode)
                {
                    System.out.println("found"); //PRINT
                    found = true; //This key is already recognized as held
                }
                else
                {
                    System.out.println("not found"); //PRINT
                    //This key is not recognized as held
                }
            }
            if(!found) //If key must be added to keysHeld
            {
                keysHeld.add(keyCode); //Add to list of held keys
            }
        System.out.println(keysHeld.toString()); //PRINT ArrayList of all held keys
    } //end of keyPressed


        public void keyReleased(KeyEvent e) //similar in concept to keyPressed
        {
         int keyCode = e.getKeyCode();

         int index = 0;
         boolean found = false;
         while(!found && index < keysHeld.size())
         {
          if(keysHeld.get(index) == (Object)keyCode)
          {
           keysHeld.remove(index); //remove key from keysHeld
           found = true;
          }
          else
          {
           index++;
          }
         }
         System.out.println(keysHeld.toString()); //PRINT ArrayList of all held keys
        } //end of keyReleased
    };
    addKeyListener( keyListnr );
}

isKeyHeld:

public boolean isKeyHeld(int e)
{
 int keyCode = e;
 Object key = (Object)keyCode;

 if(!keysHeld.isEmpty())
 {
  int index = 0;
  while(index<keysHeld.size())
  {
   if(keysHeld.get(index) == key)
   {
    return true;
   }
   index++;
  }
 }
 return false;
}

控制台输出:(持有leftArrow [37],然后按下rightArrow [39])

[37]
errorCheck: keysHeld: [37], 39 not found
errorCheck: keysHeld: [37], 39 not found
errorCheck: keysHeld: [37], 39 not found
errorCheck: keysHeld: [37], 39 not found
...

2 个答案:

答案 0 :(得分:1)

几点:

  • 您不是使用keysHeld的实例填充KeyEvent数组,而是使用从Integer keyCodes派生的autoBoxed int对象填充。
  • 如果您希望退出index
  • 中的while循环,则需要增加keyPressed变量
  • 您不应该使用==来比较Objects循环中的两个while

您可以使用以下内容进行测试:

    if(keysHeld.get(index++).equals(new Integer(keyCode))

答案 1 :(得分:0)

处理多个密钥时,最好使用keyReleased(KeyEvent)方法:它可以在密钥发布期间更轻松地处理多个密钥组合。

我注意到的是,当在keyPressed()内时,我只会捕获一个关键字符。在keyReleased上,我能够捕获多个字符(例如 CTRL - V )。