Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Super Contributor
arunbiet
Posts: 259
Registered: ‎06-22-2010
My Carrier: blackberry developer

How to lock keypad programatically

I want to lock my keypad programatically.I have gone through this link

 

http://supportforums.blackberry.com/t5/Java-Development/put-a-password-for-lock-and-unlock-divice-pr...

 

but its of no help.Can anyone suggest me a better solution

Please use plain text.
New Developer
marco_de_costantini
Posts: 6
Registered: ‎03-06-2012
My Carrier: tim

Re: How to lock keypad programatically

Hi

You have to implements a KeyListener and consume the key event.

See this usefull example:

http://supportforums.blackberry.com/t5/Java-Development/Protect-BlackBerry-applications-with-a-passw...

when you return true on "keychar", "key up " or "key down", the key event stops on your application and there is no propagation.

marco.

 

 

Please use plain text.
Super Contributor
arunbiet
Posts: 259
Registered: ‎06-22-2010
My Carrier: blackberry developer

Re: How to lock keypad programatically

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().lockSystem(true);
				 Dialog.alert("locked successfully");
				EventInjector.KeyCodeEvent ev = new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.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.KeyCodeEvent.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?

Please use plain text.
New Developer
marco_de_costantini
Posts: 6
Registered: ‎03-06-2012
My Carrier: tim

Re: How to lock keypad programatically

Simply question..have you registered the keylistener?

 

on your UIApllication:

MyScreen screen  = new MyScreen() ;
pushScreen(screen );
this.addKeyListener(screen);

 

Please use plain text.
Super Contributor
arunbiet
Posts: 259
Registered: ‎06-22-2010
My Carrier: blackberry developer

Re: How to lock keypad programatically

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

Please use plain text.
New Developer
marco_de_costantini
Posts: 6
Registered: ‎03-06-2012
My Carrier: tim

Re: How to lock keypad programatically

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 :smileyhappy:

Please use plain text.
Super Contributor
arunbiet
Posts: 259
Registered: ‎06-22-2010
My Carrier: blackberry developer

Re: How to lock keypad programatically

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().lockSystem(true);
				Dialog.alert("locked successfully");
				EventInjector.KeyCodeEvent ev = new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.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.KeyCodeEvent.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;
    }
   
}

 

Please use plain text.
Super Contributor
arunbiet
Posts: 259
Registered: ‎06-22-2010
My Carrier: blackberry developer

Re: How to lock keypad programatically

can anyone help me out

Please use plain text.