Java 2 ME GameCanvas -- 如何接收所有 keyPressed 事件?
你好
我正在写一个简单的j2me计算器。使用 GameCanvas 类作为我的界面的基础。我需要处理小键盘按键事件和游戏相关事件(例如 UP、DOWN、FIRE 等)来移动光标。问题是模拟器和我的索尼爱立信都没有收到一些数字键盘和游戏相关的事件。例如,我得到 SOFT1、SEND、2、4、ASTERISK 键按下事件,但不接收 1、3、LEFT、RIGH、UP strong>、DOWN、FIRE 事件。
class CalculatorCanvas extends GameCanvas
{
public CalculatorCanvas() throws java.io.IOException
{
super(true);
setFullScreenMode(true);
cursor = new Sprite(Image.createImage("/cursor.png"));
}
public void paint(Graphics g)
{
g.setColor(0);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.paint(g);
}
protected void keyPressed(int keyCode)
{
System.out.println("keyPressed: " + getKeyName(key));
if (keyCode == getKeyCode(RIGHT))
cursor.move(STEP, 0);
if (key == getKeyCode(LEFT))
cursor.move(-STEP, 0);
/*...*/
repaint();
}
private final int STEP = 3;
private Sprite cursor;
}
我怎样才能收到所有这些事件?
预先感谢
PS 我发现 Canvas (GameCanvas 的基类)重新接收所有这些事件,但我必须使用 GameCanvas
Hello
I'm writing simple j2me calculator. using GameCanvas class as a basis of my interface. I need to handle both numpad key events and game-relaited events (such as UP, DOWN, FIRE, etc) for moving my cursor. The problem is that neither emulator, nor my Sony Ericsson doesn't receives some numpad and game-relaited events. For example, I get SOFT1, SEND, 2, 4, ASTERISK key pressed events, but don't receive 1, 3, LEFT, RIGH, UP, DOWN, FIRE events.
class CalculatorCanvas extends GameCanvas
{
public CalculatorCanvas() throws java.io.IOException
{
super(true);
setFullScreenMode(true);
cursor = new Sprite(Image.createImage("/cursor.png"));
}
public void paint(Graphics g)
{
g.setColor(0);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.paint(g);
}
protected void keyPressed(int keyCode)
{
System.out.println("keyPressed: " + getKeyName(key));
if (keyCode == getKeyCode(RIGHT))
cursor.move(STEP, 0);
if (key == getKeyCode(LEFT))
cursor.move(-STEP, 0);
/*...*/
repaint();
}
private final int STEP = 3;
private Sprite cursor;
}
How can I receive all this events?
Thanks in advance
PS I've found that Canvas (base class for GameCanvas) reseives all this events, but I have to use GameCanvas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查 GameCanvas 构造函数的 javadoc。你应该用“false”来称呼它!
check javadoc for GameCanvas constructor. You should call it with "false" !