程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME手機鍵盤重復按鍵監聽測試例子

J2ME手機鍵盤重復按鍵監聽測試例子

編輯: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();
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved