java键盘事件问题
importjava.awt.Frame;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;p...
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Test
{
public static void main(String args[])
{
new S();
}
}
class S extends Frame implements KeyListener
{
S()
{
this.addKeyListener(this);
this.setSize(200,200);
this.setVisible(true);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == 38 && e.getKeyCode() == 39) //为什么我同时按住了向上和向右键不会输出这句话? 如果这样做不行,应该怎么改?求高手!
{
System.out.println("你按了向上键和向右键");
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
} 展开
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Test
{
public static void main(String args[])
{
new S();
}
}
class S extends Frame implements KeyListener
{
S()
{
this.addKeyListener(this);
this.setSize(200,200);
this.setVisible(true);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == 38 && e.getKeyCode() == 39) //为什么我同时按住了向上和向右键不会输出这句话? 如果这样做不行,应该怎么改?求高手!
{
System.out.println("你按了向上键和向右键");
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
} 展开
4个回答
展开全部
虽然人的感觉是两个按钮同时按了,但是程序还是会分先后的,所以程序不可能同时收到两个事件,需要另外的方法处理,一般引入一个判断的标志就行了
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Test
{
public static void main(String args[])
{
new S();
}
}
class S extends Frame implements KeyListener
{
private int flag;
S()
{
flag = 0;
this.addKeyListener(this);
this.setSize(200,200);
this.setVisible(true);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == 38 || e.getKeyCode() == 39)
{
flag += e.getKeyCode();
}
if(flag == 77)
{
System.out.println("你同时按了向上键和向右键");
}
}
public void keyReleased(KeyEvent e) {
flag = 0;
}
public void keyTyped(KeyEvent e) {}
}
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Test
{
public static void main(String args[])
{
new S();
}
}
class S extends Frame implements KeyListener
{
private int flag;
S()
{
flag = 0;
this.addKeyListener(this);
this.setSize(200,200);
this.setVisible(true);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == 38 || e.getKeyCode() == 39)
{
flag += e.getKeyCode();
}
if(flag == 77)
{
System.out.println("你同时按了向上键和向右键");
}
}
public void keyReleased(KeyEvent e) {
flag = 0;
}
public void keyTyped(KeyEvent e) {}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你需要保存以前按得键
boolean up = false; //向上键
boolean right = fase; //向右键
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case :KeyEvent.VK_UP:
up = true;
break;
case :KeyEvent.VK_RIGHT:
right = true;
break;
}
if( up && right){
System.out.println("你按了向上键和向右键");
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case :KeyEvent.VK_UP:
up = false;
break;
case :KeyEvent.VK_RIGHT:
right = false;
break;
}
}
boolean up = false; //向上键
boolean right = fase; //向右键
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case :KeyEvent.VK_UP:
up = true;
break;
case :KeyEvent.VK_RIGHT:
right = true;
break;
}
if( up && right){
System.out.println("你按了向上键和向右键");
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case :KeyEvent.VK_UP:
up = false;
break;
case :KeyEvent.VK_RIGHT:
right = false;
break;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
键盘同时只发送一个键,你每次获得的是一个键码,不是两个~~~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询