日期:2014-05-20  浏览次数:20753 次

J2ME手机键盘重复按键监听测试例子

我们知道有些手机是不支持重复按键功能,为了模拟重复按键的功能,我们不得不使用一个线程来进行处理。

废话少说,直接贴代码吧。

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();
??????? }
??? }
}