04-23-2012 02:41 AM
I want to lock my keypad programatically.I have gone through this link
but its of no help.Can anyone suggest me a better solution
04-23-2012 05:10 AM
Hi
You have to implements a KeyListener and consume the key event.
See this usefull example:
http://supportforums.blackberry.com/t5/Java-Develo
when you return true on "keychar", "key up " or "key down", the key event stops on your application and there is no propagation.
marco.
04-23-2012 05:43 AM
Thank you Macro sir,i have implemented the keyListener accordingly in my code,bt its still not working properly.Here is my code
package mypackage;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen implements KeyListener
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");
BitmapField bmField1 = new BitmapField(Bitmap.getBitmapResource("next.png"), BitmapField.FOCUSABLE) {
protected void paint(Graphics graphics) {
invalidate();
super.paint(graphics);
if (isFocus()) {
graphics.setColor(Color.BLACK);
} else {
graphics.setColor(Color.WHITE);
}
graphics.setFont(Font.getDefault().derive(0, 14));
graphics.drawText("submit", 20, 10);
};
public void onFocus(int direction) {
setBitmap(Bitmap.getBitmapResource("next_s.png"));
invalidate();
super.onFocus(direction);
}
public void onUnfocus() {
super.onUnfocus();
setBitmap(Bitmap.getBitmapResource("next.png"));
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected boolean navigationClick(int status, int time) {
//ApplicationManager.getApplicationManager().lockS ystem(true);
Dialog.alert("locked successfully");
EventInjector.KeyCodeEvent ev = new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv ent.KEY_DOWN, ((char)Keypad.KEY_LOCK), KeypadListener.STATUS_ALT);
EventInjector.invokeEvent(ev);
return true;
}
};
add(bmField1);
}
public boolean trackwheelUnclick(int status, int time) {
return false;
}
/** Invoked when the trackwheel is rolled. */
public boolean trackwheelRoll(int amount, int status, int time) {
return true;
}
public boolean keyChar(char key, int status, int time) {
//intercept the ESC key - exit the app on its receipt
boolean retval = false;
switch (key) {
case Characters.ENTER:
Dialog.alert("locked successfully");
EventInjector.KeyCodeEvent ev = new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv ent.KEY_DOWN, ((char)Keypad.KEY_LOCK), KeypadListener.STATUS_ALT);
EventInjector.invokeEvent(ev);
break;
case Characters.ESCAPE:
close();
break;
default:
retval = super.keyChar(key,status,time);
}
return retval;
}
public boolean keyDown(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyRepeat */
public boolean keyRepeat(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyStatus */
public boolean keyStatus(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyUp */
public boolean keyUp(int keycode, int time) {
return false;
}
}Can you tell me where i am going wrong?
04-23-2012 06:18 AM
Simply question..have you registered the keylistener?
on your UIApllication:
MyScreen screen = new MyScreen() ; pushScreen(screen ); this.addKeyListener(screen);
04-23-2012 06:22 AM
Yes sir i am going to MyScreen from MyApp screen
public class MyApp extends UiApplication
{
/**
* Entry point for application
* @param args Command line arguments (not used)
*/
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public MyApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new MyScreen());
this.addKeyListener(new MyScreen());
}
}There i did put
04-23-2012 06:35 AM
Hi Sir
You are adding two different instances of the same class, one onto the display stack and one as listener.
Try my previous code with only one instance so the Screen on the stack is the same that listens for key event.
let me know the result ![]()
04-23-2012 06:57 AM
Sir,after applying ur code also,the result is same,can u kindly look into the MyScreen class,whether everything is alright there
package mypackage;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen implements KeyListener
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
setTitle("MyTitle");
BitmapField bmField1 = new BitmapField(Bitmap.getBitmapResource("next.png"), BitmapField.FOCUSABLE) {
protected void paint(Graphics graphics) {
invalidate();
super.paint(graphics);
if (isFocus()) {
graphics.setColor(Color.BLACK);
} else {
graphics.setColor(Color.WHITE);
}
graphics.setFont(Font.getDefault().derive(0, 14));
graphics.drawText("submit", 20, 10);
};
public void onFocus(int direction) {
setBitmap(Bitmap.getBitmapResource("next_s.png"));
invalidate();
super.onFocus(direction);
}
public void onUnfocus() {
super.onUnfocus();
setBitmap(Bitmap.getBitmapResource("next.png"));
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected boolean navigationClick(int status, int time) {
//ApplicationManager.getApplicationManager().lockS ystem(true);
Dialog.alert("locked successfully");
EventInjector.KeyCodeEvent ev = new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv ent.KEY_DOWN, ((char)Keypad.KEY_LOCK), KeypadListener.STATUS_ALT);
EventInjector.invokeEvent(ev);
return true;
}
};
add(bmField1);
}
public boolean keyChar(char key, int status, int time) {
//intercept the ESC key - exit the app on its receipt
boolean retval = false;
switch (key) {
case Characters.ENTER:
Dialog.alert("locked successfully");
EventInjector.KeyCodeEvent ev = new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv ent.KEY_DOWN, ((char)Keypad.KEY_LOCK), KeypadListener.STATUS_ALT);
EventInjector.invokeEvent(ev);
break;
case Characters.ESCAPE:
close();
break;
default:
retval = super.keyChar(key,status,time);
}
return retval;
}
public boolean keyDown(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyRepeat */
public boolean keyRepeat(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyStatus */
public boolean keyStatus(int keycode, int time) {
return false;
}
/** Implementation of KeyListener.keyUp */
public boolean keyUp(int keycode, int time) {
return false;
}
}
04-24-2012 12:37 AM
can anyone help me out