Java 密钥适配器

发布于 2024-08-09 13:52:37 字数 2738 浏览 3 评论 0原文

我对 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;
}

控制台输出:(按住左箭头[37],然后按下右箭头[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
...

I am somewhat unfamiliar with how the Java KeyAdapter works, and am getting unexpected results with the following code using KeyAdapter. The problem occurs when a key is pressed while another key is already held down, regardless of whether isKeyPressed() is called.

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). Hopefully the thorough comments are helpful.

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

Console output: (held leftArrow[37], and then pressed 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
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-08-16 13:52:37

有几点:

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

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

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

A couple points:

  • You aren't populating your keysHeld array with instances of KeyEvent, but with autoBoxed Integer objects derived from the int keyCodes.
  • You need to increment your index variable if you want to get out of the while loop in keyPressed
  • You shouldnt use == to compare the two Objects in your while loop

You can test with something like the following:

    if(keysHeld.get(index++).equals(new Integer(keyCode))
感情旳空白 2024-08-16 13:52:37

处理多个按键时,最好使用 keyReleased(KeyEvent) 方法:它可以更轻松地在按键释放期间处理多个按键组合。

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

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.

What I've noticed was that when inside the keyPressed(), I would only get to capture one key character. On a keyReleased, I was able to capture multiple characters (such as CTRL-V).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文