Java Tutorial/J2ME/Key Event
Canvas key events
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class EventsMIDlet extends MIDlet
implements CommandListener {
private Display display;
protected boolean started;
private Command exitCommand = new Command("Exit", Command.EXIT, 0);
protected void startApp() {
if (!started) {
display = Display.getDisplay(this);
Canvas canvas = new EventsCanvas();
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
display.setCurrent(canvas);
started = true;
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
notifyDestroyed();
}
}
}
class EventsCanvas extends Canvas {
static int[] keyCodes = {KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4,
KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9,
KEY_POUND, KEY_STAR};
static String[] keyNames = {"KEY_NUM0", "KEY_NUM1", "KEY_NUM2", "KEY_NUM3", "KEY_NUM4",
"KEY_NUM5", "KEY_NUM6", "KEY_NUM7", "KEY_NUM8", "KEY_NUM9",
"KEY_POUND", "KEY_STAR"};
static int[] gameActions = {
UP, DOWN, LEFT, RIGHT, FIRE,
GAME_A, GAME_B, GAME_C, GAME_D};
static String[] gameNames = {
"UP", "DOWN", "LEFT", "RIGHT", "FIRE",
"GAME_A", "GAME_B", "GAME_C", "GAME_D" };
int lastKeyCode = 0;
int lastX;
int lastY;
boolean pointer;
protected void keyPressed(int keyCode) {
lastKeyCode = keyCode;
repaint();
}
protected void keyRepeated(int keyCode) {
lastKeyCode = keyCode;
repaint();
}
protected void keyReleased(int keyCode) {
lastKeyCode = 0;
repaint();
}
protected void pointerPressed(int x, int y) {
lastX = x;
lastY = y;
pointer = true;
repaint();
}
protected void pointerDragged(int x, int y) {
lastX = x;
lastY = y;
pointer = true;
repaint();
}
protected void pointerReleased(int x, int y) {
pointer = false;
repaint();
}
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0);
if (lastKeyCode != 0) {
String keyText = "keyCode " + lastKeyCode;
String keyName = null;
for (int i = 0; i < keyCodes.length; i++) {
if (lastKeyCode == keyCodes[i]) {
keyName = keyNames[i];
break;
}
}
if (keyName == null) {
for (int i = 0; i < gameActions.length; i++) {
if (lastKeyCode == getKeyCode(gameActions[i])) {
keyName = gameNames[i];
break;
}
}
}
g.drawString(keyText, getWidth()/2, getHeight()/2,
Graphics.BASELINE|Graphics.HCENTER);
if (keyName != null) {
g.drawString(keyName, getWidth()/2, getHeight()/2 + g.getFont().getHeight(),
Graphics.BASELINE|Graphics.HCENTER);
}
} else if (pointer) {
g.drawString("(" + lastX + ", " + lastY + ")", getWidth()/2, getHeight()/2,
Graphics.BASELINE|Graphics.HCENTER);
}
}
}
Check the key event
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class KeyMIDlet extends MIDlet {
public void startApp() {
Displayable d = new KeyCanvas();
d.addCommand(new Command("Exit", Command.EXIT, 0));
d.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
});
Display.getDisplay(this).setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class KeyCanvas extends Canvas {
private Font mFont= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
private String mMessage = "[Press keys]";
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
int x = w / 2;
int y = h / 2;
g.drawString(mMessage, x, y, Graphics.BASELINE | Graphics.HCENTER);
}
protected void keyPressed(int keyCode) {
int gameAction = getGameAction(keyCode);
switch (gameAction) {
case UP:
mMessage = "UP";
break;
case DOWN:
mMessage = "DOWN";
break;
case LEFT:
mMessage = "LEFT";
break;
case RIGHT:
mMessage = "RIGHT";
break;
case FIRE:
mMessage = "FIRE";
break;
case GAME_A:
mMessage = "GAME_A";
break;
case GAME_B:
mMessage = "GAME_B";
break;
case GAME_C:
mMessage = "GAME_C";
break;
case GAME_D:
mMessage = "GAME_D";
break;
default:
mMessage = "";
break;
}
repaint();
}
}
Game action
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class GameActionMIDlet extends MIDlet implements CommandListener {
private Command exitCommand;
Display display;
Displayable d;
public void startApp() {
Display display = Display.getDisplay(this);
d = new GameActionCanvas();
exitCommand = new Command("Exit", Command.EXIT, 1);
d.addCommand(exitCommand);
d.setCommandListener(this);
display.setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
}
class GameActionCanvas extends Canvas {
int width = 0;
int height = 0;
String aMessage = "message";
public void paint(Graphics g) {
width = getWidth();
height = getHeight();
g.setGrayScale(255);
g.fillRect(0, 0, width - 1, height - 1);
g.setGrayScale(0);
g.drawRect(0, 0, width - 1, height - 1);
g.drawString(aMessage, 10, 10, Graphics.TOP | Graphics.LEFT);
}
protected void keyPressed(int keyCode) {
int gameAction = getGameAction(keyCode);
switch (gameAction) {
case UP:
aMessage = "Up";
break;
case DOWN:
aMessage = "Down";
break;
case LEFT:
aMessage = "Left";
break;
case RIGHT:
aMessage = "Right";
break;
case FIRE:
aMessage = "Send";
break;
case GAME_A:
aMessage = "GAME_A";
break;
case GAME_B:
aMessage = "GAME_B";
break;
case GAME_C:
aMessage = "GAME_C";
break;
case GAME_D:
aMessage = "GAME_D";
break;
default:
aMessage = "";
break;
}
repaint();
}
}
Get key code
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class KeyEventsMIDlet extends MIDlet implements CommandListener {
private Command exitCommand;
Display display;
Displayable d;
public void startApp() {
Display display = Display.getDisplay(this);
d = new KeyEventsCanvas();
exitCommand = new Command("Exit", Command.EXIT, 1);
d.addCommand(exitCommand);
d.setCommandListener(this);
display.setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
}
class KeyEventsCanvas extends Canvas {
int width = 0;
int height = 0;
String aMessage = "message";
public void paint(Graphics g) {
width = getWidth();
height = getHeight();
g.setGrayScale(255);
g.fillRect(0, 0, width - 1, height - 1);
g.setGrayScale(0);
g.drawRect(0, 0, width - 1, height - 1);
g.drawString(aMessage, 10, 10, Graphics.TOP | Graphics.LEFT);
}
protected void keyPressed(int keyCode) {
aMessage = getKeyName(keyCode);
if (aMessage.equals("2"))
aMessage = "2";
repaint();
}
}