我无法让 KeyEvent 侦听器工作
好的,首先。 如果我这样做 System.out.println(e); 当我按下该键时,它会打印出来。然而,我一生都无法弄清楚如何将其存储为 int 。如果我这样做的话,我的 IDE 不会给我任何错误 int 按下 = e.KEY_PRESSED();或 int Pressed = e.getKeyCode(); 但如果我尝试打印按下什么也不会发生。
我已经尝试让它工作几个小时了,谷歌搜索 KeyEvent 处理程序和 Javadocs 似乎对我没有什么帮助。
public void keyPressed(KeyEvent e) {
pressed = e.getKeyCode();
System.out.println(pressed);
}
do{
time = System.currentTimeMillis();
do{
if(pressed == 37||pressed==38||pressed==39||pressed==40){
lastvalid=pressed;
}
}
while(System.currentTimeMillis() < time + speed);
switch(lastvalid){
case 37: catarloc.set(0, (Integer)catarloc.get(0)-1); break;
case 38: catarloc.set(1, (Integer)catarloc.get(1)-1); break;
case 39: catarloc.set(0, (Integer)catarloc.get(0)+1); break;
case 40: catarloc.set(1, (Integer)catarloc.get(1)+1); break;
}
if(Math.random() > .95 || apples < 1){
applearray[(int)(Math.random()*100/2.8)][(int)(Math.random()*100/4)] = true;
apples++;
}
score+=catarloc.size()-1;
label.setText("Score "+ score);
mainWindow.repaint();
}
while(win == false || lose == false);
Ok, first off.
If I do System.out.println(e);
that does print when I push the key. However I can not for the life of me figure out how to store this into an int. My IDE gives me no errors if I do
int pressed = e.KEY_PRESSED(); or int pressed = e.getKeyCode();
but if I try to print pressed nothing happens.
I've been trying to get this to work for hours and Googling KeyEvent handlers and the Javadocs seem to be of little help to me on this.
public void keyPressed(KeyEvent e) {
pressed = e.getKeyCode();
System.out.println(pressed);
}
do{
time = System.currentTimeMillis();
do{
if(pressed == 37||pressed==38||pressed==39||pressed==40){
lastvalid=pressed;
}
}
while(System.currentTimeMillis() < time + speed);
switch(lastvalid){
case 37: catarloc.set(0, (Integer)catarloc.get(0)-1); break;
case 38: catarloc.set(1, (Integer)catarloc.get(1)-1); break;
case 39: catarloc.set(0, (Integer)catarloc.get(0)+1); break;
case 40: catarloc.set(1, (Integer)catarloc.get(1)+1); break;
}
if(Math.random() > .95 || apples < 1){
applearray[(int)(Math.random()*100/2.8)][(int)(Math.random()*100/4)] = true;
apples++;
}
score+=catarloc.size()-1;
label.setText("Score "+ score);
mainWindow.repaint();
}
while(win == false || lose == false);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
< 怎么样?代码>keyEvent.getKeyCode()?它返回一个 int。
KEY_PRESSED
是 KeyEvent 上的 static Final int 成员,表示按下按键时的事件,而不是实际的按键代码。更新:(哎呀)我看到您已经尝试过 getKeyCode。如果打印
getKeyLocation
或getKeyChar
?What about
keyEvent.getKeyCode()
? It returns an int.KEY_PRESSED
is a static final int member on KeyEvent and represents an event when a key is pressed down, not the actual key code.Update: (Whoops) I see that you already tried getKeyCode. What happens if you print
getKeyLocation
orgetKeyChar
?根据事件类型:
keyEvent.getKeyChar()
为您提供与按键关联的字符,但仅适用于KEY_TYPED
事件。否则它会返回CHAR_UNDEFINED
keyEvent.getKeyCode()
为您提供虚拟按键代码,但仅适用于KEY_PRESSED
和KEY_RELEASED
事件。否则返回VK_UNDEFINED
。Depending on the type of event:
keyEvent.getKeyChar()
gives you the character associated with the key, but only forKEY_TYPED
events. Otherwise it returnsCHAR_UNDEFINED
keyEvent.getKeyCode()
gives you the virtual key code, but only forKEY_PRESSED
andKEY_RELEASED
events. Otherwise it returnsVK_UNDEFINED
.