在动作类游戏当中,发必杀的时候常常是同时按下多个键,手机中同样可以
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class Test extends Canvas implements Runnable{
public final static int PRESS1 = 1;
public final static int PRESS2 = 2;
public int num1 = 0;
public int num2 = 0;
public Test() {
super();
Thread t = new Thread(this);
t.start();
}
public void run()
{
while(true)
{
if(num1 == 1 && num2 == 1)
System.out.println("1和2同时被按下了");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected void paint(Graphics g) {
}
public void keyPressed(int key)
{
if(key == Canvas.KEY_NUM1)
num1 = 1;
if(key == Canvas.KEY_NUM2)
num2 = 1;
}
public void keyReleased(int key)
{
if(key == Canvas.KEY_NUM1)
num1 = 0;
if(key == Canvas.KEY_NUM2)
num2 = 0;
}
}