How do you detect a long click on a ListField component?
Do you override its navigationClick(int status, int time) and fumble with its time argument (how?) or is there some builtin method for detecting long clicks?
And more importantly - how do you display the menu (the one in the middle of the screen) once you detected such a click?
The background is that on short clicks I'd like to let the user edit the selected item. And on long clicks I'd like to display a menu in the middle of the screen to offer secondary tasks: delete item, change item display order, etc.
Below is my current test code - src\mypackage\MyList.java:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField() {
protected boolean navigationClick(int status, int time) {
System.err.println("XXX status=" + status + ", index=" + getSelectedIndex());
return true;
}
};
public MyScreen() {
setTitle("How to detect long click?");
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
}
}
Thank you
Alex
You can override the touchEvent method of your Field. Then do something like this:
ObjectListField myList = new ObjectListField() {
long touchedAt = -1;
long HOLD_TIME = 2000; // 2 seconds or whatever you define the hold time to be
protected boolean touchEvent(TouchEvent message) {
if(message.getEvent() == TouchEvent.DOWN) {
touchedAt = System.currentTimeMillis();
} else if(message.getEvent() == TouchEvent.UP) {
if(System.currentTimeMillis() - touchedAt < HOLD_TIME)
touchedAt = -1; // reset
else
//write logic you need for touch and hold
}
return true;
}
};
Please note that this is a rough implementation just to give you an idea. I only used the time coordinate here. Your implementation will probably need to factor in the X and Y coordinates of where the user touched the screen, because if he moved his finger, then that wouldn't be a touch and hold.
Related
I made a very simple test gui based on this brilliant article about getting started with Esper.
What surprises me is that this query is validated to true after the very first tick event is sent, if the price is above 6.
select * from StockTick(symbol='AAPL').win:length(2) having avg(price) > 6.0
As far as I understand, win:length(2) needs TWO ticks before an event is fired, or am I wrong?
Here is a SSCCE for this question, just press the "Create Tick Event" button and you will see the StockTick Event being fired at once.
It needs the following jars which comes bundled with Esper
esper\lib\antlr-runtime-3.2.jar
esper\lib\cglib-nodep-2.2.jar
esper\lib\commons-logging-1.1.1.jar
esper\lib\esper_3rdparties.license
esper\lib\log4j-1.2.16.jar
esper-4.11.0.jar
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Random;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPAdministrator;
import com.espertech.esper.client.EPRuntime;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.UpdateListener;
import javax.swing.JTextField;
public class Tester extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton createRandomValueEventButton;
private JPanel panel;
private JPanel southPanel;
private JPanel centerPanel;
private static JTextArea centerTextArea;
private static JTextArea southTextArea;
private static Random generator = new Random();
private EPRuntime cepRT;
private JSplitPane textSplitPane;
private JButton btnNewButton;
private static JTextField priceTextField;
public Tester() {
getContentPane().setLayout(new BorderLayout(0, 0));
JSplitPane splitPane = new JSplitPane();
createRandomValueEventButton = new JButton("Create Tick Event With Random Price");
splitPane.setLeftComponent(createRandomValueEventButton);
createRandomValueEventButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
createTickWithRandomPrice();
}
});
panel = new JPanel();
splitPane.setRightComponent(panel);
panel.setLayout(new GridLayout(1, 0, 0, 0));
btnNewButton = new JButton("Create Tick Event");
panel.add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
createTick();
}
});
priceTextField = new JTextField();
priceTextField.setText(new Integer(10).toString());
panel.add(priceTextField);
priceTextField.setColumns(4);
getContentPane().add(splitPane, BorderLayout.NORTH);
textSplitPane = new JSplitPane();
textSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
getContentPane().add(textSplitPane, BorderLayout.CENTER);
centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout(0, 0));
JScrollPane centerTextScrollPane = new JScrollPane();
centerTextScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
centerTextArea = new JTextArea();
centerTextArea.setRows(12);
centerTextScrollPane.setViewportView(centerTextArea);
southPanel = new JPanel();
southPanel.setLayout(new BorderLayout(0, 0));
JScrollPane southTextScrollPane = new JScrollPane();
southTextScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
southTextArea = new JTextArea();
southTextArea.setRows(5);
southTextScrollPane.setViewportView(southTextArea);
textSplitPane.setRightComponent(southTextScrollPane);
textSplitPane.setLeftComponent(centerTextScrollPane);
setupCEP();
}
public static void GenerateRandomTick(EPRuntime cepRT) {
double price = (double) generator.nextInt(10);
long timeStamp = System.currentTimeMillis();
String symbol = "AAPL";
Tick tick = new Tick(symbol, price, timeStamp);
System.out.println("Sending tick:" + tick);
centerTextArea.append(new Date().toString()+" Sending tick:" + tick+"\n");
cepRT.sendEvent(tick);
}
public static void GenerateTick(EPRuntime cepRT) {
double price = Double.parseDouble(priceTextField.getText());
long timeStamp = System.currentTimeMillis();
String symbol = "AAPL";
Tick tick = new Tick(symbol, price, timeStamp);
System.out.println("Sending tick:" + tick);
centerTextArea.append(new Date().toString()+" Sending tick: " + tick+"\n");
cepRT.sendEvent(tick);
}
public static void main(String[] args){
Tester tester = new Tester();
tester.setSize(new Dimension(570,500));
tester.setVisible(true);
}
private void createTickWithRandomPrice(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GenerateRandomTick(getEPRuntime());
}
});
}
private void createTick(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GenerateTick(getEPRuntime());
}
});
}
private void setupCEP(){
Configuration cepConfig = new Configuration();
cepConfig.addEventType("StockTick", Tick.class.getName());
EPServiceProvider cep = EPServiceProviderManager.getProvider("myCEPEngine", cepConfig);
cepRT = cep.getEPRuntime();
EPAdministrator cepAdm = cep.getEPAdministrator();
EPStatement cepStatement = cepAdm.createEPL(
"select * from " +
"StockTick(symbol='AAPL').win:length(2) " +
"having avg(price) > 6.0");
cepStatement.addListener(new CEPListener());
//System.out.println("cepStatement.getText(): "+cepStatement.getText());
}
private EPRuntime getEPRuntime(){
public static class Tick {
String symbol;
Double price;
Date timeStamp;
public Tick(String s, double p, long t) {
symbol = s;
price = p;
timeStamp = new Date(t);
}
public double getPrice() {return price;}
public String getSymbol() {return symbol;}
public Date getTimeStamp() {return timeStamp;}
#Override
public String toString() {
return symbol+" Price: " + price.toString();
}
}
public static class CEPListener implements UpdateListener {
}
Actually aggregation and conditions are independent of how many events are in data window. There are functions you could use to check whether a data window is "filled": the "leaving", "count" or "prevcount" for example.
For anyone interested,
changing the query to this solved the problem
select * from StockTick(symbol='AAPL').win:length_batch(2) having avg(price) > 6.0 and count(*) >= 2
Now an event will be triggered for every consecutive tick with the price higher than 6, in batches of two.
ObjectChoiceField not working because the below code:
protected boolean navigationUnclick(int status, int time) {
return true;
}
I have added this code to remove the menus on touchevent. Means I have made a custom bottom tab and adding and deleting verticalfields on screen. When i touch any HorizontalField it shows menu that's why i added the above code.
This is my code contains ObjectChoiceField added in horizontalfield:
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import com.ec.pleasewaitpopup.PleaseWaitPopupScreen;
import com.np.naijapings.ApplicationFont;
import com.np.naijapings.Constant;
import com.np.naijapings.TabControlScreen;
import com.np.naijapings.intermediateclasses.GetUserListIntermediator;
import com.tv.servercommunication.WebServiceDetails;
public class FindUsersScreen extends VerticalFieldManager implements FieldChangeListener{
private VerticalFieldManager _mainVfm;
private VerticalFieldManager _contentVfm;
private BitmapField _headerBmp;
private EncodedImage _bitmap;
private LabelField _gender;
private LabelField _age;
private LabelField _religion;
private ObjectChoiceField _genderChoiceField;
private ObjectChoiceField _ageChoiceField;
private ObjectChoiceField _religionChoiceField;
private ButtonField _findUser;
private static String[] _genderChoices={"Both gender","Male","Female"};
private static String[] _ageChoices={"Any age","18-25","26-30","31-35","36-45","46-50"};
private String[] _religionChoices={"Any religion","Hindu","Muslim"};
public FindUsersScreen(){
//HEADER IMG
_bitmap = EncodedImage.
getEncodedImageResource("find-user_header.png");
_headerBmp = new BitmapField(Constant.sizePic(_bitmap, _bitmap.getHeight(), _bitmap.getWidth()));
//MAIN VFM
_mainVfm=new VerticalFieldManager();
//CONTENT VFM
final Bitmap tabBackGroundImage = Bitmap
.getBitmapResource("finduserscr_bg.png");
_contentVfm=new VerticalFieldManager(){
protected void paint(Graphics graphics) {
int y = FindUsersScreen.this.getManager().getVerticalScroll();
graphics.drawBitmap( 0, y, tabBackGroundImage.getWidth(), tabBackGroundImage.getHeight(), tabBackGroundImage, 0, 0 );
super.paint( graphics );
}
};
//CREATE WIDGETS
_gender=new LabelField("Gender");
_genderChoiceField=new ObjectChoiceField("Gender", _genderChoices,0){
protected boolean touchEvent(TouchEvent message) {
return super.touchEvent(message);
}
};
_age=new LabelField("Age");
_ageChoiceField=new ObjectChoiceField("Age", _ageChoices,0);
_religion=new LabelField("Religion");
_religionChoiceField=new ObjectChoiceField("Religion", _religionChoices,0);
_findUser=new ButtonField(" Find Users ",ButtonField.CONSUME_CLICK);
_findUser.setChangeListener(this);
//SET FONT TYPE
/*_gender.setFont(ApplicationFont.labelFont_16);
_genderChoiceField.setFont(ApplicationFont.labelFont_16);
_ageChoiceField.setFont(ApplicationFont.labelFont_16);
_age.setFont(ApplicationFont.labelFont_20);
_religionChoiceField.setFont(ApplicationFont.labelFont_20);
_religion.setFont(ApplicationFont.labelFont_20);
*/
//SET MARGIN
/*_gender.setMargin(5,20,5,20);
_age.setMargin(5,20,5,20);
_religion.setMargin(5,20,5,20);
*/
_contentVfm.setMargin(15,30,15,0);
_genderChoiceField.setMargin(10,5,5,5);
_religionChoiceField.setMargin(10,5,5,5);
_ageChoiceField.setMargin(10,5,5,5);
_findUser.setMargin(10,80,20,80);
_contentVfm.setMargin(30,10,30,10);
//ADD FIELDS TO CONTENT VFM
//_contentVfm.add(_gender);
_contentVfm.add(_genderChoiceField);
//_contentVfm.add(_age);
_contentVfm.add(_ageChoiceField);
//_contentVfm.add(_religion);
_contentVfm.add(_religionChoiceField);
_contentVfm.add(_findUser);
_mainVfm.add(_headerBmp);
_mainVfm.add(_contentVfm);
add(_mainVfm);
}
public void fieldChanged(Field field, int context) {
if(field==_findUser){
Object obAgeRange = _ageChoiceField.getChoice(_ageChoiceField.getSelectedIndex());
String ageRange = obAgeRange.toString();
Object obgender = _genderChoiceField.getChoice(_genderChoiceField.getSelectedIndex());
String gender = obgender.toString();
Object obReligion = _religionChoiceField.getChoice(_religionChoiceField.getSelectedIndex());
String religion = obReligion.toString();
GetUserListIntermediator getUserListIntermediator=new GetUserListIntermediator(ageRange,gender,religion);
PleaseWaitPopupScreen.showScreenAndWait(getUserListIntermediator, Constant.PLEASE_WAIT_TEXT);
}
}
}
could anyone answer me how to solve this problem.
You don't get drop-down list because the field code does not process unclick event. You have intercepted and consumed this event (via return true;) before your field can process it.
Try this code for your event handler.
protected boolean navigationUnclick(int status, int time) {
super.navigationUnclick(status, time);
return true;
}
I have created a CustomToolTip with reference to
earlier questions i asked on stack
Custom pop Up
and
Dialog creation
Now , i have created a CustomisedToolTip like as follows
But the issue is the whole screen gets displayed or occupied the space
i need such that the TextField on the previous screen be Active for that moment
The code for Customised Tool Tip i generated is as follows
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
public class ToolTip extends PopupScreen{
protected void applyTheme() {
}
private static VerticalFieldManager vfm=new VerticalFieldManager(Manager.NON_FOCUSABLE|Manager.FIELD_HCENTER){
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0,0,getWidth(),getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0,0,getWidth(),getHeight());
super.paint(graphics);
}
};
private LabelField lbl;
private static int xPosition;
private static int yPosition;
private String message;
private static Bitmap toolTipImg;
private static BitmapField toolTipBmpFld;
private static ButtonField button;
public ToolTip(final String message,int xPos,int yPos,final Bitmap toolTipImg){
super(vfm);
this.xPosition=xPos;
this.yPosition=yPos;
this.message=message;
this.toolTipImg=toolTipImg;
button=new ButtonField(message,ButtonField.NON_FOCUSABLE){
protected void paint(Graphics graphics) {
graphics.drawBitmap(0,0, toolTipImg.getWidth(), toolTipImg.getHeight(), toolTipImg, 0, 0);
super.paint(graphics);
}
protected void layout(int width, int height) {
super.layout(width, height);
setExtent( toolTipImg.getWidth(), toolTipImg.getHeight());
}
};
vfm.add(button);
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setPosition(xPosition, yPosition);
}
protected boolean keyChar(char c, int status, int time) {
// TODO Auto-generated method stub
if(c==Characters.ESCAPE)
{
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
return super.keyChar(c, status, time);
}
}
Can i get a pop up like this in Blackberry
Overriding the protected void paint(Graphics graphics) method of MainScreen can be helpful in this case.
When a Field get focus, you can tell the MainScreen (parent screen) to draw a tooltip on specified position after it completes it own drawing. And when the Field lost focus, you can avoid tooltip painting. Also a timer can be introduced for removing the tooltip after some time.
Check following implementation. The implementation needs to be improved, currently it's only represents my idea.
public final class MyScreen extends MainScreen implements TooltipProtocol {
private TooltipProtocol tooltipProtocolInstance;
public MyScreen() {
tooltipProtocolInstance = this;
ButtonField bfOne = getButtonField("ButtonField One", "Tooltip One", 200, 20);
ButtonField bfTwo = getButtonField("ButtonField Two", "Tooltip Two", 200, 60);
add(bfOne);
add(bfTwo);
}
private ButtonField getButtonField(String text, final String tooltiptext, final int x, final int y) {
ButtonField bf = new ButtonField(text) {
protected void onFocus(int direction) {
tooltipProtocolInstance.showToolTipText(tooltiptext, x, y);
super.onFocus(direction);
}
protected void onUnfocus() {
tooltipProtocolInstance.hideToolTipText();
super.onUnfocus();
}
};
return bf;
}
private String toolTipText;
private int xTooptip;
private int yTooptip;
private Timer tooltipTimer;
public void showToolTipText(String text, int x, int y) {
toolTipText = text;
xTooptip = x;
yTooptip = y;
if (tooltipTimer != null) {
tooltipTimer.cancel();
tooltipTimer = null;
}
tooltipTimer = new Timer();
tooltipTimer.schedule(new TimerTask() {
public void run() {
hideToolTipText();
}
}, 2000);
invalidate();
}
public void hideToolTipText() {
toolTipText = null;
invalidate();
}
protected void paint(Graphics graphics) {
super.paint(graphics);
if (toolTipText != null) {
int oldColor = graphics.getColor();
graphics.setColor(Color.GREEN);
graphics.drawText(toolTipText, xTooptip, yTooptip);
graphics.setColor(oldColor);
}
}
}
interface TooltipProtocol {
public void showToolTipText(String text, int x, int y);
public void hideToolTipText();
}
Following are output of the above code:
When first button got focus
When first button lost focus, second button got focus
When timer hides tooltip of the second button
As far I understand the question, you want to create a tool tip kind popup screen, but you want user to interact with your LoginScreen UI elements without blocking the whole screen by the tool tip.
This cannot be achievable in Blackberry. PropupScreen acts like a dialog. As we are pushing the instance of the popup screen to the screen stack to make it visually appear, it always block the delegate screen. Unless you remove the popup from the screen stack you cannot interact with your LoginScreen UI components, though they are visually available.
Visit Blackberry java docs for more details: http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/ui/container/PopupScreen.html
Edit:1
You can create a ToolTip screen giving the timer. You can google around and i am sure you will get many code samples. I have listed one link below.
http://v4ks1n.wordpress.com/2011/01/28/tooltips-class-for-blackberry/
How can I lock the keypad in Blackberry application using "lockSystem" method in J2ME ??
And also the brightness of the blackberry should reduce to Zero ??
its really easy. I know the answer. We can just use the method "lockSystem". I have coded as following to lock the keypad. It takes long time for me to find it, but u got this.
package net.asem;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class LockBlackberry extends UiApplication
{
public static void main(String[] args)
{
LockBlackberry lockB = new LockBlackberry();
lockB.enterEventDispatcher();
}
LockBlackberry()
{
pushScreen(new myBlackBerryClass());
}
}
final class myBlackBerryClass extends MainScreen implements FieldChangeListener<br>
{
LabelField title;
ButtonField btn1;
myBlackBerryClass()
{
LabelField title = new LabelField("Title : Locking the Device.",LabelField.USE_ALL_WIDTH | LabelField.USE_ALL_WIDTH);
setTitle(title);
btn1 = new ButtonField("KeyPad Loack ?",ButtonField.CONSUME_CLICK);
btn1.setChangeListener(this);
add(btn1);
}
public void fieldChanged(Field field, int context)
{
if(field == btn1)
{
Click();
}
}
private void Click()
{
ApplicationManager manager = ApplicationManager.getApplicationManager();
manager.lockSystem(true);
}
}
I am new to BlackBerry App development. I want to be able to listen for keypress events whenever the BlackBerry (8900 in my case) is on and on all screens is this possible?
If so, it would be great for someone to direct me in the right direction. I am already having a look at Interface KeyListener.
import net.rim.device.api.system.*;
Thanks all
Implement a keylistenerClass like:
import model.Profile;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
public final class ShortcutHandler implements KeyListener {
public boolean keyChar(char key, int status, int time) {
return false;
}
public boolean keyDown(int keycode, int time) {
if (Keypad.KEY_ESCAPE == Keypad.key(keycode)) {
// Consume the event.
// Here I'm consuming the event for the escape key
return true;
}
//let the system to pass the event to another listener.
return false;
}
public boolean keyRepeat(int keycode, int time) {
return false;
}
public boolean keyStatus(int keycode, int time) {
return false;
}
public boolean keyUp(int keycode, int time) {
return false;
}
}
Then in your application constructor
public Application() {
//Add the listener to the system for this application
addKeyListener(new ShortcutHandler());
}
I confirm that it's working when the application is in the background.
As I understood, you want to listen to all key events in all applications running on device, not only in your application.
I think it's not possible.
UPDATE
How does the volume up and down key work? – Abs 11 hours ago
If you want to say that all applications receive key events from volume keys, thats not true. RIM OS will receive those events and then update all audio components like alert, audio, player etc.
you can easely check it with this sample:
Do following:
run sample
enter some key events
look at events number
go background
enter some key events
go back to sample by menu->switch application
check events number, it still the same
Code:
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
public class KeyListenerApp extends UiApplication implements KeyListener {
Scr mScreen;
public KeyListenerApp() {
mScreen = new Scr();
pushScreen(mScreen);
addKeyListener(this);
}
public static void main(String[] args) {
KeyListenerApp app = new KeyListenerApp();
app.enterEventDispatcher();
}
private void updateScreen(final String text) {
mScreen.addLine(text);
}
public boolean keyChar(char key, int status, int time) {
updateScreen("keyChar " + key);
return true;
}
public boolean keyDown(int keycode, int time) {
updateScreen("keyDown " + keycode);
return true;
}
public boolean keyRepeat(int keycode, int time) {
updateScreen("keyRepeat " + keycode);
return true;
}
public boolean keyStatus(int keycode, int time) {
updateScreen("keyStatus " + keycode);
return true;
}
public boolean keyUp(int keycode, int time) {
updateScreen("keyUp " + keycode);
return true;
}
}
class Scr extends MainScreen {
int mEventsCount = 0;
LabelField mEventsStatistic = new LabelField("events count: "
+ String.valueOf(mEventsCount));
public Scr() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
add(mEventsStatistic);
}
public void addLine(final String text) {
getApplication().invokeLater(new Runnable() {
public void run() {
mEventsStatistic.setText("events count: "
+ String.valueOf(++mEventsCount));
insert(new LabelField(text), 1);
}
});
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(goBGMenuItem);
}
MenuItem goBGMenuItem = new MenuItem("go backgroun", 0, 0) {
public void run() {
getApplication().requestBackground();
}
};
}
This how I imagine it could work
create application which extends UiApplication or even Application
create an implementation of Keylistener (which could also extend Thread if you want)
add your KeyListener implementation to your application via addKeyListener()
Then do whatever you want.
The code given above certainly works, but there is a catch. You wont be able to trap the key presses on native apps like call handling sms incoming browsing and stuff. As system generates global event to these apps. Its like you are able to define a routine for clicks when your app is in background , but the functionality of that routine is localised to your application only. It wont effect other apps as such.