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 ?
tambunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
why not just do a msgbox and they have to press the ok button or pres enter
0
tambunAuthor Commented:
Could you please show me a code to display msgbox ?
0
tambunAuthor Commented:
Oh, I am using J2ME / MIDP , not normal java
0
OBCTCommented:
Try using something like this....

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

   while (done == 0)
   {
      try
      {
         wait();
      }
      catch (InterruptedException e) { e.printStackTrace(); }
   }
}

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

Have a look at this link for more details on dealing with threads.
http://java.sun.com/docs/books/tutorial/essential/threads/index.html

Even though your using J2ME, the thread principals still apply.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhprCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Wireless Networking

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.