Tutorials Blackberry KeyListener in Blackberry

KeyListener in Blackberry

In my previous post i have explained how to handle events that occur on BlackBerry fields but it is also very important to know how to handle events from the device hardware interface such as the keypad and trackball.
This capability is also beneficial for game development where often the keys of the device become the controls. we need to tell the application which keys control what functions.Here is a sample example to show the keypad and trackball to control specific functions of a typical game scenario. The five operations that can take place in this game scenario are:

  • Move Left
  • Move Right
  • Move Up
  • Move Down
  • Shoot

Take a look at the code sample below which will explain you the same.

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.system.KeypadListener;
import net.rim.device.api.system.KeyListener;

class KeypadListenerExample extends UiApplication {

    private static RichTextField command; 

    KeypadListenerExample() {   
        MainScreen mainScreen = new MyScreen();
        command = new RichTextField("Waiting for command...");
        mainScreen.add(command);      
        pushScreen(mainScreen);
    }

    public static void main(String[] args) {
        KeypadListenerExample app = new KeypadListenerExample();
        app.enterEventDispatcher();
    }     

    static class MyScreen extends MainScreen {
        public boolean keyChar(char key, int status, int time) {
            if(key == 'd'){
                command.setText("Move Left");
            }else if(key == 'j'){
                command.setText("Move Right");
            }else if(key == 't'){
                command.setText("Move Up");
            }else if(key == 'b'){
                command.setText("Move Down");
            }else if(key == 'g'){
                command.setText("Shoot!");
            }
            return true;
        }
        protected boolean navigationMovement(int dx, int dy, int status, int time) {
            if(dx < 0 && dy == 0){
                command.setText("Move Left");
            }else if(dx > 0 && dy == 0){
                command.setText("Move Right");
            }else if(dx == 0 && dy > 0){
                command.setText("Move Up");
            }else if(dx == 0 && dy < 0){
                command.setText("Move Down");
            }          
            return true;       
        }
        protected boolean navigationClick(int status, int time) {
            command.setText("Shoot!");
            return true;
        }
    }

}

There are various methods used in this example to get user inputs that are all found inside the MyScreen class which is the MainScreen. These methods all override their parent methods which are part of the Screen class. The keyCar method deals with user input commands from the keypad and the other two methods prepended with “navigation” deal with user input from the trackball. Taking this approach enables the user to use the input device that they feel most comfortable with (keypad or trackball).

The keyChar method simply detects the character that was submitted to the device by comparing the key parameter. The other input parameters are not needed for this occasion. The status parameter can tell us information such as whether the shift or caps lock inputs are enabled. The time parameter is the number of milliseconds since the device was turned on. Our implementation of this method simply changes the RichTextField on the screen to read the command operation. We return true because this informs that the event was consumed. If we wanted all the key presses to behave as normal then we would need to invoke the superclass with the alternative code:

return super.keyChar(key, status, time);
Total
0
Shares
Previous Post
Blackberry Tutorials

FocusChangeListener in blackberry

Next Post
Blackberry Tutorials

Custom LabelField in Blackberry

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All