03-11-2009 11:47 AM
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?
Solved! Go to Solution.
03-17-2009 08:47 AM
03-17-2009 09:28 AM - edited 03-17-2009 09:53 AM
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.
03-23-2009 04:50 PM
03-23-2009 05:25 PM
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.
03-27-2009 01:38 PM
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).
03-27-2009 01:50 PM
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.KeyCodeEv
ent.KEY_DOWN, fire, 0, 0)); EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv
ent.KEY_DOWN, a, 0, 0)); EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv
ent.KEY_DOWN, b, 0, 0)); EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv
ent.KEY_DOWN, c, 0, 0)); EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEv
ent.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.
04-02-2009 09:43 AM
04-02-2009 09:52 AM