Link to home
Start Free TrialLog in
Avatar of tambun
tambun

asked on

Waiting a keypress

I want to create somekind of "Press Any Key" method for my Midlet.

I created a class like this :

class Wait extends Canvas {

    private String message;
    private int done = 0;

    Wait() {
    }

    void start (String msg) {
       done = 0;
       message = msg;
       repaint();
       serviceRepaints();

       while (done == 0) {
       }

    }

    protected void keyPressed(int code) {
       done = 1;
    }

    protected void paint(Graphics g) {
       g.drawString(message, 10, 50, Graphics.LEFT|Graphics.TOP);
    }

}

And I called from my main Midlet like this :

   Wait w = new Wait();
   w.start("Press any key");
   display.setCurrent(w);

But it just locked the process :)

I believe that because of the while loop can not catch the keyPressed. Anyone know any workaround ?
Avatar of Mikal613
Mikal613
Flag of United States of America image

why not just do a msgbox and they have to press the ok button or pres enter
Avatar of tambun
tambun

ASKER

Could you please show me a code to display msgbox ?
Avatar of tambun

ASKER

Oh, I am using J2ME / MIDP , not normal java
ASKER CERTIFIED SOLUTION
Avatar of OBCT
OBCT

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi tambun,

is this OK?

class Wait extends Canvas {

    private String message;
    private int done = 0;

    Wait() {
    }

    void start (String msg) {
       done = 0;
       message = msg;
       repaint();
       serviceRepaints();

       while (done == 0) {
              try { Thread.sleep(100); } catch (Exception _e){ //handle }
       }

    }

    protected void keyPressed(int code) {
       done = 1;
    }

    protected void paint(Graphics g) {
       g.drawString(message, 10, 50, Graphics.LEFT|Graphics.TOP);
    }

}


-bhpr