We know that low-level event handling in MIDP is handled through keypressed (), keyreleased () and keyrepeated (), which are triggered when keystrokes are pressed, released, and repeated. When the method is called, the system will pass the key value of the key to the above three methods, according to the key value of the keys we can do related processing. The following key values are defined in MIDP: KEY_NUM0, Key_num1, key_num2, key_num3, Key_num4, KEY_NUM5, KEY_NUM6, Key_num7, KEY_NUM8, KEY_NUM9
, Key_star and Key_pound. In the game development in order to ensure the portability of the program, usually we will convert the key value to game action, in MIDP defined the following game action: Up, down, left, right, FIRE, Game_a, Game_b, Game_c,game_d.
The conversion is very simple and can be getgameaction () by the canvas provided by the method. Generally it is easier to process keypressed () and keyreleased (), but it is slightly more complicated to handle keystrokes that have been pressed down. Because the device we use does not necessarily support the event of consecutive keystrokes. You can use the method hasrepeatevents () to detect whether the platform supports repetitive events when keys are pressed continuously.
If supported then you can handle the relevant logic in the Keyrepeated () method, and if not, then you have to take other approaches. Here, the author introduces a method to handle the continuous key by setting the sign bit. In fact, the principle is very simple, we set the flag to determine whether the button is pressed, such as we judge left is not pressed. When left is pressed, we set the member variable leftpressed to true with the following code: public void keypressed (int keycode) {int action = Getgamea
Ction (KeyCode);
Switch (action) {case left:left ();
Leftpressed = true; BreaK
Case Right:right ();
Rightpressed = true;
Break
Default:break;
} repaint ();
When the key is released, we set the relevant tag bit to false.
public void keyreleased (int keycode) {int action = getgameaction (KeyCode);
Switch (action) {case left:leftpressed = false;
buttonpressed = "";
Break
Case right:rightpressed = false;
buttonpressed = "";
Break
Default:break;
} repaint ();
This way we can paint the screen again based on the state of the marker bit: if (leftpressed) {left ();
} if (rightpressed) {right (); The author gives a simple example to demonstrate that we make a midlet, when the user presses left, the J2ME string moves to the left-hand side, and when the user presses right, the J2ME string moves to the right-hand side. For the sake of simplicity, I did not deal with down and up.
The following is the application source code.
Import javax.microedition.lcdui.*; Import Javax.microedition.midlet.MIDlet;
Import javax.microedition.midlet.MIDletStateChangeException;
public class Keyactionmidlet extends MIDlet {private display display;
Private Maincanvas Maincanvas;
protected void startApp () throws midletstatechangeexception {display = Display.getdisplay (this);
Maincanvas = new Maincanvas ();
New Thread (Maincanvas). Start ();
Display.setcurrent (Maincanvas); } protected void Pauseapp () {} protected void Destroyapp (Boolean arg0) throws Midletstatechangeexcepti
on {}} package Com.j2medev;
Import javax.microedition.lcdui.*;
public class Maincanvas extends Canvas implements Runnable {private String buttonpressed;
Private Boolean leftpressed;
Private Boolean rightpressed;
private int px = GetWidth ()/2;
public final int py = getheight ()/2;
Public Maincanvas () {buttonpressed = "";
private void Left () {if (px >= 0) { px--;
} buttonpressed = "Left";
Repaint ();
private void Right () {if (px <= getwidth ()) {px++;
} buttonpressed = "right";
Repaint (); public void Run () {while (true) {if (leftpressed) {L
EFT ();
} if (rightpressed) {right ();
try {thread.sleep (50);
catch (Interruptedexception e) {e.printstacktrace ();
}} public void Paint (Graphics g) {G.setcolor (0xFFFFFF);
G.fillrect (0, 0, getwidth (), getheight ());
G.setcolor (0x000000); g.DrawString (buttonpressed, Graphics.left |
Graphics.top); g.DrawString ("J2me", px, py, Graphics.hcenter |
Graphics.top); } public void keyreleased (intkeycode) {int action = getgameaction (KeyCode);
Switch (action) {case left:leftpressed = false;
buttonpressed = "";
Break
Case right:rightpressed = false;
buttonpressed = "";
Break
Default:break;
} repaint ();
The public void keypressed (int keycode) {int action = getgameaction (KeyCode);
Switch (action) {case left:left ();
Leftpressed = true;
Break
Case Right:right ();
Rightpressed = true;
Break
Default:break;
} repaint ();
The public void keyrepeated (int keycode) {int action = getgameaction (KeyCode);
Switch (action) {case left:left ();
Break
Case Right:right ();Break
Default:break;
} repaint ();
}
}