简体   繁体   English

Java KeyAdapter

[英]Java KeyAdapter

I am somewhat unfamiliar with how the Java KeyAdapter works, and am getting unexpected results with the following code using KeyAdapter . 我有点不熟悉Java KeyAdapter工作方式,并且使用KeyAdapter使用以下代码获得了意外的结果。 The problem occurs when a key is pressed while another key is already held down, regardless of whether isKeyPressed() is called. 不管是否isKeyPressed() ,在按下另一个键的同时按下一个键时,都会发生此问题。

Note: I know this is a lot of code, and I apologize. 注意:我知道这是很多代码,对不起。 I tried the best I could to isolate it, and I think it resides primarily around the comments in the keyHandler method below (how keyHandler() puts the keys currently pressed into keysHeld ). 我尽力隔离了它,我认为它主要位于下面keyHandler方法中的注释周围( keyHandler()如何将当前按下的键放到keysHeld )。 Hopefully the thorough comments are helpful. 希望详尽的评论对您有所帮助。

keyHandler: 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: 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;
}

Console output: (held leftArrow[37], and then pressed rightArrow[39]) 控制台输出:(按住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
...

A couple points: 几点:

  • You aren't populating your keysHeld array with instances of KeyEvent , but with autoBoxed Integer objects derived from the int keyCodes. 您不会使用KeyEvent实例填充keysHeld数组,而是使用从int keyCodes派生的autoBoxed Integer对象。
  • You need to increment your index variable if you want to get out of the while loop in keyPressed 如果要退出keyPressedwhile循环,则需要增加index变量
  • You shouldnt use == to compare the two Objects in your while loop 您不应该使用==来比较while循环中的两个Objects

You can test with something like the following: 您可以使用以下内容进行测试:

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

When handling multiple keys, it's best to use the keyReleased(KeyEvent) method: it makes it easier to handle multiple key combinations during a key release. 处理多个键时,最好使用keyReleased(KeyEvent)方法:它使在释放键期间更容易处理多个键组合。

What I've noticed was that when inside the keyPressed() , I would only get to capture one key character. 我注意到的是,在keyPressed() ,我只能捕获一个关键字符。 On a keyReleased , I was able to capture multiple characters (such as CTRL - V ). keyReleased ,我能够捕获多个字符(例如CTRL - V )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM