Key events in MIDlets

N

Nikhil

Hello,

I am basically new to J2ME MIDP development and also new to this
group. So apologies if this has already been covered or turns out to
be a stupid question :) Here goes:
I am trying to develop a MIDlet where I have a Form to accept certain
values from the user. This is fairly straightforward and is easy to
create with the high level Form a and TextField objects.
Now, I wish to add a functionality to my MIDlet, in which, the user
can press a specific key.. say the "Select" key or the "Call" key.. at
any time during the form to start another thread of operation which
performs something totally different... say for example, the user is
entering values into the form and in between somewhere he presses this
specific key and the phone start playing music. I hope you get my
point.
Basically I want to listen for key pressed or key held events on a
Form object. Now I understand that Key events can be found only on the
low-level Canvas based screens. So is it possible to do it with Screen
elements..?
Thanks,

Nik
 
J

JScoobyCed

Nikhil said:
Hello,

I am basically new to J2ME MIDP development and also new to this
group. So apologies if this has already been covered or turns out to
be a stupid question :) Here goes:
I am trying to develop a MIDlet where I have a Form to accept certain
values from the user. This is fairly straightforward and is easy to
create with the high level Form a and TextField objects.
Now, I wish to add a functionality to my MIDlet, in which, the user
can press a specific key.. say the "Select" key or the "Call" key.. at
any time during the form to start another thread of operation which
performs something totally different... say for example, the user is
entering values into the form and in between somewhere he presses this
specific key and the phone start playing music. I hope you get my
point.
Basically I want to listen for key pressed or key held events on a
Form object. Now I understand that Key events can be found only on the
low-level Canvas based screens. So is it possible to do it with Screen
elements..?
Thanks,

Nik

You might have alook at the javax.microedition.lcdui.Command object and
CommandListener.
<sniplet>
public class myForm implements CommandListener {

Command startCommand = null;
public myForm() {
// Creates a command that:
// - displays "Start new Thread"
// - is displayed at the place of the standard OK button
// - is displayed at first in the list of button (if supported)
startCommand = new Command("Start new Thread", Command.OK, 1);
addCommand(startCommand);
setCommandListener(this);

public void commandAction(Command cmd, Displayable d) {
if(cmd==startCommand) {
doStuff();
}
}
}
</sniplet>

Now if you want to control some graphical items of the MIDlet without
using Command on the Form, you can use a List or other Item subclasses
which can have ItemCommandListener to use.
 
D

Darryl L. Pierce

Nikhil said:
Basically I want to listen for key pressed or key held events on a
Form object. Now I understand that Key events can be found only on the
low-level Canvas based screens. So is it possible to do it with Screen
elements..?

No. If you look at the APIs you'll see that the key functions (keyPressed(),
keyReleased(), keyRepeated()) are introduced in the
javax.microedition.lcdui.Canvas class and that Form doesn't inherit from
that. When you're working with high-level UI widgets you have no access to
any low-level UI events like key presses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top