本文介绍了Java KeyListener-如何检测是否按下了任何键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将KeyListener添加到TextArea,并希望检查是否按下了任何键.我有以下内容,但检查所有字母和数字太笨拙了:
I've added a KeyListener to a TextArea and wish to check if any key is pressed down.I have the following but it's too clumsy to check for all the letters and numbers:
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_B ||
e.getKeyCode() == KeyEvent.VK_C ||e.getKeyCode() == KeyEvent.VK_D etc...){
}
}
推荐答案
您不需要任何if语句.每次按下键都会触发keyPressed
方法,因此您会自动陷入该方法中.
You wouldn't need any if statements. The keyPressed
method is fired whenever a key is pressed, so you're automatically thrown into the method.
这篇关于Java KeyListener-如何检测是否按下了任何键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!