For example, if we want 'C' to be hotkey in 'Cancel', then we will need to postfix character 'C' by unicode character '\u0332'; so in this case 'Cancel' will be as 'C\u0332ancel'.
To implement the hotkey functionality we need to override keyChar method of Screen class and give action on specified character.
Following is blackberry code which implements hotkey O and C for OK and Cancel buttons respectively.
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
/**
*
Main class of the application.
*/
public class Hotkey extends UiApplication
{
//statics ------------------------------------------------------------------
public static void main(String[] args)
{
Hotkey theApp = new Hotkey();
//To make the application enter the event thread and start processing messages, we invoke the enterEventDispatcher method
theApp.enterEventDispatcher();
}
public Hotkey()
{
pushScreen(new HotkeyTestScreen());
}
}
class HotkeyTestScreen extends MainScreen
{
private KButtonField m_oOKButtonField;
private KButtonField m_oCancelButtonField;
public HotkeyTestScreen()
{
super();
setTitle("Hotkey Test");
add(m_oOKButtonField = new KButtonField("O\u0332K"));
add(m_oCancelButtonField = new KButtonField("C\u0332ancel"));
}
protected boolean keyChar(char key, int status, int time)
{
boolean bReturn = true;
if(('O' == key) || ('o' == key))
{
m_oOKButtonField.fieldChangeNotify(0);
}
else if(('C' == key) || ('c' == key))
{
m_oCancelButtonField.fieldChangeNotify(0);
}
else
{
bReturn = super.keyChar(key, status, time);
}
return bReturn;
}
class KButtonField extends ButtonField
{
KButtonField(String strLabel)
{
super(strLabel);
}
public void fieldChangeNotify(int context)
{
Status.show(getLabel() + " button clicked!");
}
}
}
No comments:
Post a Comment