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.

inside custom component

Java Development

Reply
Developer
almeida
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified
Accepted Solution

Equivalent of Canvas.getKeyCode without Using Canvas

I need to get the key codes that correspond to the various game actions on a device.  This information is available from the getKeyCode() method of the Canvas class.  I would like to be able to get this information without using Canvas.  The method isn't static, so I actually need a Canvas object, but I can't construct the object (IllegalStateException) because I don't have an application instance at the time of construction.

 

Any ideas? 

Please use plain text.
Administrator
MSohm
Posts: 14,362
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Equivalent of Canvas.getKeyCode without Using Canvas

Are you trying to capture key input while your application is in the background?  This is not supported on BlackBerry handhelds.  Can you further describe your end goal if this isn't the case?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
almeida
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Equivalent of Canvas.getKeyCode without Using Canvas

[ Edited ]

Thanks for your reply.  I am not trying to capture key presses.  I'm actually doing the opposite: I am writing a CLDC application that simulates key presses using EventInjector.  I would like to be able to inject key presses that correspond to the Java game actions (e.g., GAME_FIRE, GAME_A, etc.).  The goal is to be able to control a Java game running the foreground with my CLDC application that is running in the background.  Because the keycodes corresponding to the Java game actions are device dependent, in order to do this the right way, I have to do something like this when my application starts:

 

 

Canvas canvas = new Canvas() {

protected void paint(Graphics graphics) {

}

};

char fire = (char) canvas.getKeyCode(Canvas.FIRE);

char a = (char) canvas.getKeyCode(Canvas.GAME_A);

char b = (char) canvas.getKeyCode(Canvas.GAME_B);

char c = (char) canvas.getKeyCode(Canvas.GAME_C);

char d = (char) canvas.getKeyCode(Canvas.GAME_D);

 

That works okay, but I have to be careful when I call that code.  If it gets called too early in the startup process, "new Canvas() {...}" throws an IllegalStateException because I don't have an Application instance yet.  Ideally, I'd like to just avoid using the Canvas class altogether.

Message Edited by almeida on 03-17-2009 09:53 AM
Please use plain text.
Administrator
MSohm
Posts: 14,362
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Equivalent of Canvas.getKeyCode without Using Canvas

You can use the ApplicationManager.inStartup method to determine if the BlackBerry handheld is still starting up.  Once this is complete you can display a GUI.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
almeida
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Equivalent of Canvas.getKeyCode without Using Canvas

Thanks Mark, but that doesn't quite solve my problem.  I'm writing a CLDC application that extends UiApplication.  It usually runs in the background, but I guess it's not exactly a "background application."  Sorry for the confusion.

 

In the constructor of my UiApplication, I'd like to get the characters associated the Java game actions.  The following code does not work because I don't have an Application instance yet in the constructor of Example.

 

public final class Example extends UiApplication

private final char fire, a, b, c, d;

 

Example() {

...

 

final Canvas canvas = new Canvas() {

protected void paint(final Graphics graphics) {

}

};

 

fire = (char) canvas.getKeyCode(Canvas.FIRE);

a = (char) canvas.getKeyCode(Canvas.GAME_A);

b = (char) canvas.getKeyCode(Canvas.GAME_B);

c = (char) canvas.getKeyCode(Canvas.GAME_C);

d = (char) canvas.getKeyCode(Canvas.GAME_D);

 

...

}

 

public static void main(final String[] args) {

final Example example = new Example();

example.enterEventDispatcher();

}

}

 

Unless there is an equivalent to getKeyCode that doesn't use a Canvas, I think my only option is to wait for the activate callback in Example to do my initialization work.

 

Please use plain text.
Administrator
MSohm
Posts: 14,362
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Equivalent of Canvas.getKeyCode without Using Canvas

If you have create a UiApplication you shouldn't use Canvas.  Ui elements are not compatible between CLDC and MIDlet applications.

 

The Characters class contains constants for all characters.  You can use these with the Keylistener.keyChar method.  Keypad.keycode can be used to map a character to a keycode (passed into the KeyListener.keyUp and keyDown methods).

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
almeida
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Equivalent of Canvas.getKeyCode without Using Canvas

I'm not mixing UI elements.  The GUI components of my application are pure CLDC.  I never try to display the Canvas.  The only reason I am using Canvas at all is to get the Java game characters.  The Characters class doesn't have Java game characters defined, so it is of no use to me in this situation.  Let me expand my example above.

 

public final class Example extends UiApplication

private final char fire, a, b, c, d;

 

Example() {

...

 

final Canvas canvas = new Canvas() {

protected void paint(final Graphics graphics) {

}

};

 

fire = (char) canvas.getKeyCode(Canvas.FIRE);

a = (char) canvas.getKeyCode(Canvas.GAME_A);

b = (char) canvas.getKeyCode(Canvas.GAME_B);

c = (char) canvas.getKeyCode(Canvas.GAME_C);

d = (char) canvas.getKeyCode(Canvas.GAME_D);

 

...

 

EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, fire, 0, 0));

EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, a, 0, 0));

EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, b, 0, 0));

EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, c, 0, 0));

EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, d, 0, 0));

}

 

public static void main(final String[] args) {

final Example example = new Example();

example.enterEventDispatcher();

}

 

 

I want to simulate Java game characters.  The characters corresponding to FIRE, GAME_A, etc. vary between devices, so I don't want to hardcode anything in my application. The only way I can find to get those characters at run-time is to use the Canvas class.  My question is whether the characters are defined elsewhere.  If they were in the Characters class, that would be enormously helpful, but I don't see them there. 

Please use plain text.
Administrator
MSohm
Posts: 14,362
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Equivalent of Canvas.getKeyCode without Using Canvas

These values are only available within the Canvas class.  I have sent this feedback to our product management team (to add these constants to the Characters class).
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
almeida
Posts: 91
Registered: ‎02-04-2009
My Device: Not Specified

Re: Equivalent of Canvas.getKeyCode without Using Canvas

Thanks Mark.  I appreciate your help.
Please use plain text.