Skip to main content

Firing a command button manually via MenuList

2 replies [Last post]
shaneosborne
Offline
Joined: 2010-02-22
Points: 0

Good morning

Been trying to work out this issue for a while, I simply want either the enter key and/or the game fire event to cause.

Via KeyReleased I can identify the enter key or fire button as keycode 10 but I want to fire one of my commands which I have added in the short cut menu.

any idea how ?

Thanks

Shane

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shankar_vn
Offline
Joined: 2009-11-03
Points: 0

Any specific reason for firing a command after capturing game fire event?

I'll do the task in this way.

[code]
addGameKeyListener(Display.GAME_FIRE, form); //form implements ActionListener

public void actionPerformed(ActionEvent ae) {
if ((ae.getSource() instanceof Command &&
ae.getCommand().getId() == CMD_ID_COMMON) || Display.GAME_FIRE == ae.getKeyEvent()) {
//do the common task
}
}
[/code]
Never hard code keycodes, unless you are writing the application for specific devices.

shaneosborne
Offline
Joined: 2010-02-22
Points: 0

Well I got around it by doing the following

public void keyReleased(int keyCode) {
int action = com.sun.lwuit.Display.getInstance().getGameAction(keyCode);

if (action == Display.GAME_FIRE)
{
Command defaultCmd = this.getCommand(0);
super.dispatchCommand(defaultCmd,new ActionEvent(defaultCmd, keyCode));
}
super.keyReleased(keyCode);
}

Not sure if this is exactly right or the correct way of doing this but seems to work eventually!! Took a few days to get this to work this way.

The logic is all done in the main midlet and not in this form which is in a seperate class.

Thanks

Shane