我們知道有些手機是不支持重復按鍵功能,為了模擬重復按鍵的功能,我們不得不使用一個線程來進行處理。
廢話少說,直接貼代碼吧。
package com.srk.local.key.demo1;
import Javax.microedition.lcdui.Canvas;
public class NewEvent implements Runnable{
KeyEventCanvas kec;
protected boolean swtichPonit;
protected byte[] keyCode;
byte index;
public NewEvent(KeyEventCanvas kec) {
this.kec = kec;
keyCode = new byte[2];
}
public synchronized void startAction(int keyCode) {
this.keyCode[index] = (byte)keyCode;
index = (byte)((index+1)%this.keyCode.length);
}
public synchronized void stopAction(int keyCode) {
for(int i=0;i<=this.keyCode.length-1;i++) {
if(this.keyCode[i]==(byte)keyCode){
this.keyCode[i] =0;
break;
}
}
}
public synchronized boolean getSwtichPoint(){
this.swtichPonit = false;
for(int i=0;i<=this.keyCode.length-1;i++){
if(this.keyCode[i]!=0){
this.swtichPonit=true;
break;
}
}
return this.swtichPonit;
}
public synchronized byte[] getKeyCode(){
return this.keyCode;
}
public void run() {
byte[] temp = this.getKeyCode();
while(true) {
while(this.getSwtichPoint()) {
for(int i=0;i<=temp.length-1;i++){
if(temp[i]!=0) {
switch(kec.getGameAction(temp[i])){
case Canvas.UP:
kec.py --;
break;
case Canvas.DOWN:
kec.py ++;
break;
case Canvas.LEFT:
kec.px--;
break;
case Canvas.RIGHT:
kec.px++;
break;
default:
break;
}
}
}
kec.repaint();
try {
Thread.sleep(10);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.srk.local.key.demo1;
import Javax.microedition.lcdui.Canvas;
import Javax.microedition.lcdui.Graphics;
import Javax.microedition.lcdui.CommandListener;
import Javax.microedition.lcdui.Command;
import Javax.microedition.lcdui.Displayable;
public class KeyEventCanvas extends Canvas implements CommandListener {
public int px, py;
private NewEvent ne;
private Command exitCommand;
public KeyEventCanvas() {
ne = new NewEvent(this);
exitCommand = new Command("退出", Command.EXIT, 1);
this.addCommand(exitCommand);
this.setCommandListener(this);
new Thread(ne).start();
}
public void keyPressed(int keyCode) {
ne.startAction(keyCode);
}
public void keyReleased(int keyCode) {
ne.startAction(keyCode);
}
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(0xff0000);
g.fillRect(px, py, 8, 8);
}
public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
KeyMainMIDlet.quitApp();
}
}
}
package com.srk.local.key.demo1;
import Javax.microedition.lcdui.CommandListener;
import Javax.microedition.midlet.MIDlet;
import Javax.microedition.midlet.MIDletStateChangeException;
import Javax.microedition.lcdui.Command;
import Javax.microedition.lcdui.Displayable;
import Javax.microedition.lcdui.Display;
public class KeyMainMIDlet extends MIDlet implements CommandListener {
private Command exitCommand;
private KeyEventCanvas currentDisplay;
private static KeyMainMIDlet kmildlet;
public KeyMainMIDlet() {
kmildlet = this;
exitCommand = new Command("退出",Command.EXIT,1);
currentDisplay = new KeyEventCanvas();
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(kmildlet).setCurrent(currentDisplay);
protected void pauseApp() {
}
protected void destroyApp(boolean _boolean) throws
MIDletStateChangeException {
}
public static void quitApp() {
try {
kmildlet.destroyApp(false);
kmildlet.notifyDestroyed();
} catch(MIDletStateChangeException e) {
e.printStackTrace();
}
}
public void commandAction(Command command, Displayable displayable) {
if(command == exitCommand){
kmildlet.quitApp();
}
}
}