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 ?
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 ?
why not just do a msgbox and they have to press the ok button or pres enter
ASKER
Could you please show me a code to display msgbox ?
ASKER
Oh, I am using J2ME / MIDP , not normal java
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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