通常低級事件的處理是為了滿足游戲開發的需要,因為游戲開發相關的問題非常復雜。因此不在這裡進行闡述,有愛好的朋友可以和我索要相關的游戲開發資料。MIDP定義了如下鍵值在Canvas類內:
KEY_NUM0
KEY_NUM1
KEY_NUM2
KEY_NUM3
KEY_NUM4
KEY_NUM5
KEY_NUM6
KEY_NUM7
KEY_NUM8
KEY_NUM9
KEY_STAR
KEY_POUND
這些都是按鍵基本值,當然有些手機是觸摸屏的。事件的傳遞是通過如下方法來實現的。
protected void keyPressed(int keyCode)
protected void keyReleased(int keyCode)
protected void keyRepeated(int keyCode)
protected void pointerPressed(int x, int y)
protected void pointerDragged(int x, int y)
protected void pointerReleased(int x, int y)
protected void showNotify()
protected void hideNotify()
protected abstract void paint(Graphics g)
commandAction() method of the CommandListener interface
開發者應該繼續Canvas類的同時覆蓋相應的方法來實現低級事件的處理,低級事件的處理是連續的、串行的,在前面的事件返回之前後面的事件不會執行,當然也有例外,你可以參考API doc得到答案。下面是一個例子,它的功能就是在屏幕上顯示出你按下鍵的鍵值,界面非常簡單因此沒有抓圖。代碼如下所示:
//LowLevelMIDlet.Java
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/*
* Created on 2004-6-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class LowLevelMIDlet extends MIDlet
{
private Display display;
private KeyCanvas keyCanvas;
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException
{
// TODO Auto-generated method stub
display = Display.getDisplay(this);
keyCanvas = new KeyCanvas();
display.setCurrent(keyCanvas);
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp()
{
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
// TODO Auto-generated method stub
}
}
//KeyCanvas.java
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
/*
* Created on 2004-6-25
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class KeyCanvas extends Canvas implements CommandListener
{
private String key = "Press any key";
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g)
{
// TODO Auto-generated method stub
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 0);
g.drawString(key, getWidth() / 2, getHeight() / 2, Graphics.TOP
Graphics.HCENTER);
}
public void keyPressed(int keyCode)
{
key = keyCode + " is pressed";
repaint();
}
public void keyReleased(int keyCode)
{
key = keyCode + " is released";
repaint();
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1)
{
// TODO Auto-generated method stub
}
}