Java 密钥适配器
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几点:
KeyEvent
的实例填充keysHeld
数组,而是使用从int 派生的 autoBoxed
键码。Integer
对象来填充keysHeld
数组。keyPressed
中的while
循环,则需要增加您的index
变量,== 来比较
while
循环中的两个Objects
您可以使用如下内容进行测试:
A couple points:
keysHeld
array with instances ofKeyEvent
, but with autoBoxedInteger
objects derived from theint
keyCodes.index
variable if you want to get out of thewhile
loop inkeyPressed
==
to compare the twoObjects
in yourwhile
loopYou can test with something like the following:
处理多个按键时,最好使用
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 akeyReleased
, I was able to capture multiple characters (such as CTRL-V).