Get keypressed events in MIDlet

Hi i have a class which extends MIDlet, and have a form.

I want to do some action when the center-OK key is pressed within the form.

What can i do since in MIDlet keyPressed() event is never called?
Who is Participating?
PAQ'd, 100 points refunded.
Community Support Moderator
You can either add CommandAction or handle raw key press.
MrTerrenceAuthor Commented:

maybe i repeat my question again

According to wat i noe,

setCommandListener only listen to events of the soft buttons, which triggers the commandAction function. While canvas listen to any low level key events, and trigger the keyPressed() event.

But i want my MIDlet form listen to keypressed, to do action when user clicked button on the keypad like "OK"...numeric keypad..etc...

wat shd i do?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

>setCommandListener only listen to events of the soft buttons, which triggers the commandAction function
Yes thats true but the buttons that trigger the commandAction method will vary from phone to phone or vendor to vendor.
If theres an "OK" button, I would be supprised if it didn't trigger a commandAction.

May I ask what your trying to do?
MrTerrenceAuthor Commented:
i m working on an motorola iDEN phone.

Wat i want to do is v v simple actually:

There's a Main menu form and a HELP screen form.

From the main menu MIDLet user can go into the HELP screen to see some explaination. There's a soft button "BACK" for user to go back to the main menu. I just want the screen to go BACK to main menu also if the user pressed the centre OK button besides the command button "BACK" simple as tat! But failed...bcoz no response at commandAction side when any keys (like 1.2.3...directions..ok..) other than the command buttons are pressed.
Unfortunately the only 2 options are the ones you already know of.
Either create a Canvas and handle things that way or use the 'standard' back button.
As I said before, command button implementations vary from phone to phone. Because of this, people become accustomed to using the same buttons for every application. Changing this may cause confusion to certain users.
MrTerrenceAuthor Commented:
What about the CUstom Item?

I m not v sure but looks like if i can create a custom item and put it in the Form then that item can capture keyPressed() events?

Yes, this would work perfectly as long as you're using MIDP2.0.

>Unfortunately the only 2 options are the ones you already know of.

Ignore that comment :-p

This would be alot simpler than creating a Canvas AFAIK because there is a bunch of much needed functionality ready for you to use.
My appologies for not having noticed this before.
I think that should work as well.
MrTerrenceAuthor Commented:
Hi all, i have confirmed that the only solution is to use custom item.

But it is pretty challenging also since the item must be always in focus in order to get the keyPressed() event. therefore i actually gave up the idea to capture centre-ok button key in a MIDlet Form.

But, to noe more abt how to capture keyPressed event using custom item, there's a discussion abt this issue here:

I think this should be PAQ'd because MrTerrence did find a solution (although not a particularly easy one) and this could certain help other people in the future.
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.

All Courses

From novice to tech pro — start learning today.